java TLS发送电子邮件 [英] java TLS Sending Email
本文介绍了java TLS发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import javax.mail.*;
import javax.mail.Message;
import java.util.*;
import javax.mail.internet.*;
public class Sendemail {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
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)
推荐答案
你好
关于使用Gmail帐户发送电子邮件,你可以参考流动的代码(使用TLS连接):
Hello
About sending email using Gmail account, you can refer the flowing code (using TLS connection):
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 SendEmailDemo {
public static void main(String[] args) {
final String username = "username@gmail.com";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to-email@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
或者,如果您想通过Gmail SMTP发送电子邮件服务器使用SSL连接,试试这个:
Or, if you want to send an Email via Gmail SMTP server using SSL connection, try this:
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 SendEmailDemo {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username",
"password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to@gmail.com"));
message.setSubject("Testing");
message.setText("Mail content");
Transport.send(message);
System.out.println("Sent");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
我已经对它们进行了测试。他们没事。
希望这些代码片段可以帮到你。
I''ve tested both of them. They''re ok.
Hope those code snippets can help you.
这篇关于java TLS发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文