Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - send SMTP mail using JavaMail with gmail account

 
Thread Tools Search this Thread
Old 02-04-2008, 04:50 AM   #11
Default


Quote:
Originally Posted by Mritunjay
OK Here is working code. I followed the instructions here, which, though it
does not even mention SMTP, still works with SMTP:

http://www.javaworld.com/javatips/jw-javatip115.html

Look for the xxxxx occurances and substiture with your credentials :

/*
* 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;

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 = "A test from gmail";
private static final String emailFromAddress = "";
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private static final String[] sendTo = { ""};


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);
System.out.println("Sucessfully Sent mail to All Users");
}

public void sendSSLMessage(String recipients[], String subject,
String message, String from) 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("xxxxxx", "xxxxxx");
}
});

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 and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
}



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
malbers is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

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




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46