![]() |
|
|
|||||||
![]() |
Java - send SMTP mail using JavaMail with gmail account |
|
|
Thread Tools | Search this Thread |
|
|
#11 | |
|
Quote:
Hello, How do i send attachment with smtp mail . can u please tell me the code regards, Mritunjay[/quote] here is the same code working with an attatchment: /* * Created on Feb 21, 2005 * */ import java.security.Security; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; //---------------------------------------------------------- import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMultipart; import javax.mail.Multipart; import java.io.IOException; //---------------------------------------------------------- public class GoogleTest { private static final String SMTP_HOST_NAME = "smtp.gmail.com"; private static final String SMTP_PORT = "465"; private static final String emailMsgTxt = "Test Message Contents"; private static final String emailSubjectTxt = "subject line"; private static final String emailFromAddress = ""; private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; private static final String[] sendTo = {""}; private static final String fileToSend = "filename goes here"; private static final String gMailAccount = "xxxxx"; private static final String gMailPassword = "xxxxx"; public static void main(String args[]) throws Exception { Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); new GoogleTest().sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress, fileToSend, gMailAccount, gMailPassword); //System.out.println("Sucessfully Sent mail to All Users"); } public void sendSSLMessage(String recipients[], String subject, String message, String from, String attatchment, final String account, final String password) 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"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(account, password); } } ); //session.setDebug(debug); Message 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); // Setting the Subject Type msg.setSubject(subject); //---------------------------------------------------- Multipart mp = new MimeMultipart(); MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText(message); mp.addBodyPart(mbp1); MimeBodyPart mbp2 = new MimeBodyPart(); try { //attatch the file mbp2.attachFile(attatchment); } catch (IOException ioex) { ioex.printStackTrace(); } mp.addBodyPart(mbp2); msg.setContent(mp); //------------------------------------------------------ Transport.send(msg); } } malbers |
||
|
|
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| I have become rich in 30 days | lemony-snicket | A+ Certification | 2 | 09-07-2009 03:01 PM |
| How to turn $6 to $16000 in few days of web crawling | please@dontreply.net | DVD Video | 0 | 02-02-2007 07:25 AM |
| This is incredible! | jc_ice | DVD Video | 1 | 08-13-2006 10:47 AM |
| TURN $5 INTO $15,000 IN ONLY 30 DAYS...HERES HOW! | mosquitonose@hotmail.com | DVD Video | 1 | 01-19-2006 12:58 AM |
| TURN $5 INTO $15,000 IN ONLY 30 DAYS...HERES HOW! | mosquitonose@hotmail.com | DVD Video | 0 | 01-18-2006 10:32 PM |