Results 1 to 4 of 4
Thread: Java Mail Api Problem
- 06-21-2010, 08:17 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 87
- Rep Power
- 0
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
Thank you in advance :)Java 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; } }
- 06-21-2010, 01:16 PM #2
Have you tried debugging your code by adding println() statements to see where the code is executing?
Where do you create the sendingMail object?
- 06-21-2010, 05:08 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 87
- Rep Power
- 0
well i made a simple GUI in that i call sendMail when send button is clicked
- 06-21-2010, 05:19 PM #4
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:Java Code:try{throw new Exception("Who called");}catch(Exception x) {x.printStackTrace();}
Similar Threads
-
Java Mail Multipart Problem
By lowell108 in forum Advanced JavaReplies: 0Last Post: 01-04-2010, 02:36 PM -
a problem about java mail client program
By lunarstyle in forum AWT / SwingReplies: 9Last Post: 12-19-2009, 04:14 PM -
problem with sending mail usin javaX.mail api
By sandeepsai39 in forum New To JavaReplies: 4Last Post: 11-25-2009, 05:37 AM -
Problem in sending mail from java
By npoorni in forum Advanced JavaReplies: 1Last Post: 06-30-2009, 04:09 PM -
Java mail problem Urgent
By sundarjothi in forum Advanced JavaReplies: 0Last Post: 06-02-2008, 11:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks