-
Java Mail Api Problem
Well i developed this code and try to send mail with attachments using Mail API. My problem is this when i send the mail its sends 2 copies of it i.e. 2 mails are sent with same content . Below is my code please help me
Code:
import java.security.Security;
import java.util.Properties;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
public class sendingMail
{
private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final String SMTP_PORT = "465";
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String MsgTxt=null;
String Subject=null;
String From=null;
String pwd=null;
String[] too;
String[] sss;
static int count=0;
public sendingMail(String fr,String msg,String sub,String[] fro,String p,String[] ss)
{
MsgTxt=msg;
Subject=sub;
From=fr;
pwd=p;
too=fro;
sss=ss;
send();
}
public String send()
{
String s="";
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
try
{
sendSSLMessage(too,Subject,MsgTxt,From,pwd,sss);
}catch(Exception e)
{
s=e.getMessage();
}
if(s.equals(""))
{
s="Your message is successfully mailed";
}
return(s);
}
public void sendSSLMessage(String[] recipients, String subject,String message, String from,String pwd,String[] ssss) throws MessagingException
{
boolean debug = true;
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
final String from1=from;
final String pwd1=pwd;
Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator()
{protected PasswordAuthentication getPasswordAuthentication()
{return new PasswordAuthentication(from1,pwd1);}});
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i <recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
MimeBodyPart mbp1 = new MimeBodyPart ( ) ;
mbp1.setText ( message ) ;
MimeBodyPart attachment[]=new MimeBodyPart[sss.length];
for(int i=0;i<sss.length;i++)
{
attachment[i] = new MimeBodyPart() ;
FileDataSource fds = new FileDataSource ( sss[i] ) ;
attachment[i].setDataHandler ( new DataHandler ( fds ) ) ;
attachment[i].setFileName ( fds.getName() ) ;
}
Multipart mp = new MimeMultipart ( ) ;
mp.addBodyPart ( mbp1 ) ;
for(int j=0;j<sss.length;j++)
{
mp.addBodyPart ( attachment[j] ) ;
}
// msg.setContent(message, "text/plain");
msg.setContent(mp);
Transport.send(msg);
from=null;
recipients=null;
pwd=null;
subject=null;
message=null;
}
}
Thank you in advance :)
-
Have you tried debugging your code by adding println() statements to see where the code is executing?
Where do you create the sendingMail object?
-
well i made a simple GUI in that i call sendMail when send button is clicked
-
Have you tried debugging your code by adding println() statements to see where the code is executing?
Also if you can't search the source to find who is calling, add the following line where you want to know who is calling that method: Code:
try{throw new Exception("Who called");}catch(Exception x) {x.printStackTrace();}