|
import javax.mail.*;
import javax.mail.Message;
import java.util.*;
import javax.mail.internet.*;
public class Sendemail {
public static void main(String[] args) {
final String username="username";
final String password="password";
Properties prop=new Properties();
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("myemail@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("receiveremail@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,sNo spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
Please Help Someone..
I have Exception On this code
javax.mail.MessagingException: 502 5.5.1 Unrecognized command. nw9sm29708787pbb.42
at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:2080)
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1910)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:652)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at sendemail.Sendemail.main(Sendemail.java:44)
|
|
|
|
|
Hi renishkhunt,
Check it out :
<ahref="http: www.codeproject.com="" answers="" 520604="" javaplustlsplussendingplusemail#answer1"="">java TLS Sending Email[^]
|
|
|
|
|
Dear Phat (Phillip) H. VU
I use your code that is generate Exception that like
javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/plain; charset=us-ascii
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)
at javax.mail.Transport.send0(Transport.java:189)
at javax.mail.Transport.send(Transport.java:118)
at semdemail.Main.main(Main.java:46)
Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/plain; charset=us-ascii
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:870)
at javax.activation.DataHandler.writeTo(DataHandler.java:301)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:636)
... 3 more
|
|
|
|
|
Hi renishkhunt,
I've re-tested the code in my answer at java TLS Sending Email[^]. And it run as well( using gmail account to send test email to another gmail account).
Updated : I've test my demo on JDK 1.5, 1.6, 1.7 ; sending email from Gmail to another mail service, such as Hotmail.It run as well.
You can download my demo at http://sdrv.ms/VxAqON[^]
modified 3-Jan-13 6:21am.
|
|
|
|
|
You might probably to check the proxy... Hmm.
|
|
|
|