错误:
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. a5sm29349940pbw.4 - gsmtp
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
at javax.mail.Transport.send0(Transport.java:191)
at javax.mail.Transport.send(Transport.java:120)
at com.conceptbuild.EmailGenerator.main(EmailGenerator.java:120)
代码:
package com.conceptbuild;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class EmailGenerator {
public EmailGenerator() {
// TODO Auto-generated constructor stub
}
private class MailAuthenticator extends Authenticator {
String username = "username";
String password = "password";
public MailAuthenticator() {
// TODO Auto-generated constructor stub
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
public static void main(String[] args) {
String host="smtp.gmail.com";
String from="fromaddress";
String to="toaddress";
String cc="ccaddress";
Address fromaddress=null,toaddress=null,ccaddress=null;
Properties mailproperties= new Properties();
mailproperties.put("mail.smtp.host", host);
mailproperties.put("mail.smtp.port", 25);
mailproperties.put("mail.debug", "true");
mailproperties.put("mail.transport.protocal", "smtps");
mailproperties.put("mail.smtp.STARTTLS.enable", "true");
mailproperties.put("mail.smtp.auth", "true");
mailproperties.put("mail.smtps.**ssl.enable", "false");
mailproperties.setProperty("mail.smtps.**ssl.required", "false");
EmailGenerator emailgenerator= new EmailGenerator();
MailAuthenticator auth= emailgenerator.new MailAuthenticator();
Session session = Session.getInstance(mailproperties, auth);
try {
fromaddress= new InternetAddress(from,"Sushant Wadjikar");
toaddress= new InternetAddress(to);
ccaddress=new InternetAddress(cc);
}catch (AddressException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
} catch (UnsupportedEncodingException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
Message message= new MimeMessage(session);
try {
message.setFrom(fromaddress);
message.setRecipient(RecipientType.TO, toaddress);
message.setRecipient(RecipientType.CC, ccaddress);
message.setSubject("Hello !!! My First Mail from my java program");
message.setSentDate(new Date());
message.setText("Good Morning"+"\n"+"This is my first mail, sending it from my java program"+"\n"+"Thanks and Regards"+"\n"+"Sushant");
Transport transport;
transport = session.getTransport("smtp");
transport.connect();
message.saveChanges();
transport.send(message);
System.out.println("Mail send successfully.........");
} catch (MessagingException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
}