通过 Java 和 Postfix 服务器发送 SMTP 邮件
Sending SMTP Mail via Java and Postfix Server
我正在尝试使用 java 发送 smtp 邮件。我的邮件代码:
Properties properties = System.getProperties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable","true");
properties.put("mail.smtp.host", "example.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.user", "webmaster@example.com");
properties.put("mail.smtp.password", "123456");
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("webmaster@example.com","123456");
}
};
Session mailSession = Session.getInstance(properties, auth);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport("smtp");
example.com 是我的后缀服务器主机名。
我在 LINK 中创建了我的服务器,步骤完全相同:
http://cnedelcu.blogspot.com.tr/2014/01/how-to-set-up-simple-mail-server-debian-linux.html
我得到那个 java 错误:
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "example.com", port 587, isSSL false
220 example.com ESMTP Postfix (Debian/GNU)
DEBUG SMTP: connected to host "example.com", port: 587
EHLO VGate
250-example.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "10240000"
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO VGate
14:41:49,841 ERROR SystemUsers:253 - Unknown System ErrSor at REST Service
javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1420)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1408)
at com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:847)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:384)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
这里还有我的邮件日志:
Mar 2 14:43:12 VGate postfix/smtpd[28565]: connect from localhost[127.0.0.1]
Mar 2 14:43:12 VGate postfix/smtpd[28565]: SSL_accept error from localhost[127.0.0.1]: 0
Mar 2 14:43:12 VGate postfix/smtpd[28565]: warning: TLS library problem: error:14094416:SSL routines:SSL3_READ_BYTES:sslv3 alert certificate unknown:s3_pkt.c:1294:SSL alert number 46:
Mar 2 14:43:12 VGate postfix/smtpd[28565]: lost connection after STARTTLS from localhost[127.0.0.1]
Mar 2 14:43:12 VGate postfix/smtpd[28565]: disconnect from localhost[127.0.0.1]
我应该怎么做才能克服这些错误。
感谢您的帮助。
这对我有用。
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaEmail {
Properties emailProperties;
Session mailSession;
MimeMessage emailMessage;
public static void main(String args[]) throws AddressException,
MessagingException {
JavaEmail javaEmail = new JavaEmail();
javaEmail.setMailServerProperties();
javaEmail.createEmailMessage();
javaEmail.sendEmail();
}
public void setMailServerProperties() {
String emailPort = "587";//gmail's smtp port
emailProperties = System.getProperties();
emailProperties.put("mail.smtp.port", emailPort);
emailProperties.put("mail.smtp.auth", "true");
emailProperties.put("mail.smtp.starttls.enable", "true");
}
public void createEmailMessage() throws AddressException,
MessagingException {
String[] toEmails = { "xyz@pqr.com" };
String emailSubject = "Java Email";
String emailBody = "This is an email sent by JavaMail api.";
mailSession = Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);
for (int i = 0; i < toEmails.length; i++) {
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
}
emailMessage.setSubject(emailSubject);
emailMessage.setContent(emailBody, "text/html");//for a html email
//emailMessage.setText(emailBody);// for a text email
}
public void sendEmail() throws AddressException, MessagingException {
String emailHost = "smtp.gmail.com";
String fromUser = "your emailid here";//just the id alone without @gmail.com
String fromUserEmailPassword = "your email password here";
Transport transport = mailSession.getTransport("smtp");
transport.connect(emailHost, fromUser, fromUserEmailPassword);
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
transport.close();
System.out.println("Email sent successfully.");
}
}
来自 JavaMail 常见问题解答:
此外,您使用的是非常旧版本的JavaMail,如果可能请upgrade。
您的 JavaMail 实现给您错误,因为它说它找不到受信任的(或由受信任的 CA 签名的)证书。为了修复它,您必须在 java 安装中使用 "keytool" 并将您的服务器证书添加到受信任的证书列表中:
keytool -import -alias <some alias> -file <your certificate>.cer -keystore cacerts
我正在尝试使用 java 发送 smtp 邮件。我的邮件代码:
Properties properties = System.getProperties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable","true");
properties.put("mail.smtp.host", "example.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.user", "webmaster@example.com");
properties.put("mail.smtp.password", "123456");
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("webmaster@example.com","123456");
}
};
Session mailSession = Session.getInstance(properties, auth);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport("smtp");
example.com 是我的后缀服务器主机名。
我在 LINK 中创建了我的服务器,步骤完全相同: http://cnedelcu.blogspot.com.tr/2014/01/how-to-set-up-simple-mail-server-debian-linux.html
我得到那个 java 错误:
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "example.com", port 587, isSSL false
220 example.com ESMTP Postfix (Debian/GNU)
DEBUG SMTP: connected to host "example.com", port: 587
EHLO VGate
250-example.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "10240000"
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO VGate
14:41:49,841 ERROR SystemUsers:253 - Unknown System ErrSor at REST Service
javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1420)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1408)
at com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:847)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:384)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
这里还有我的邮件日志:
Mar 2 14:43:12 VGate postfix/smtpd[28565]: connect from localhost[127.0.0.1]
Mar 2 14:43:12 VGate postfix/smtpd[28565]: SSL_accept error from localhost[127.0.0.1]: 0
Mar 2 14:43:12 VGate postfix/smtpd[28565]: warning: TLS library problem: error:14094416:SSL routines:SSL3_READ_BYTES:sslv3 alert certificate unknown:s3_pkt.c:1294:SSL alert number 46:
Mar 2 14:43:12 VGate postfix/smtpd[28565]: lost connection after STARTTLS from localhost[127.0.0.1]
Mar 2 14:43:12 VGate postfix/smtpd[28565]: disconnect from localhost[127.0.0.1]
我应该怎么做才能克服这些错误。
感谢您的帮助。
这对我有用。
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaEmail {
Properties emailProperties;
Session mailSession;
MimeMessage emailMessage;
public static void main(String args[]) throws AddressException,
MessagingException {
JavaEmail javaEmail = new JavaEmail();
javaEmail.setMailServerProperties();
javaEmail.createEmailMessage();
javaEmail.sendEmail();
}
public void setMailServerProperties() {
String emailPort = "587";//gmail's smtp port
emailProperties = System.getProperties();
emailProperties.put("mail.smtp.port", emailPort);
emailProperties.put("mail.smtp.auth", "true");
emailProperties.put("mail.smtp.starttls.enable", "true");
}
public void createEmailMessage() throws AddressException,
MessagingException {
String[] toEmails = { "xyz@pqr.com" };
String emailSubject = "Java Email";
String emailBody = "This is an email sent by JavaMail api.";
mailSession = Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);
for (int i = 0; i < toEmails.length; i++) {
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
}
emailMessage.setSubject(emailSubject);
emailMessage.setContent(emailBody, "text/html");//for a html email
//emailMessage.setText(emailBody);// for a text email
}
public void sendEmail() throws AddressException, MessagingException {
String emailHost = "smtp.gmail.com";
String fromUser = "your emailid here";//just the id alone without @gmail.com
String fromUserEmailPassword = "your email password here";
Transport transport = mailSession.getTransport("smtp");
transport.connect(emailHost, fromUser, fromUserEmailPassword);
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
transport.close();
System.out.println("Email sent successfully.");
}
}
来自 JavaMail 常见问题解答:
此外,您使用的是非常旧版本的JavaMail,如果可能请upgrade。
您的 JavaMail 实现给您错误,因为它说它找不到受信任的(或由受信任的 CA 签名的)证书。为了修复它,您必须在 java 安装中使用 "keytool" 并将您的服务器证书添加到受信任的证书列表中:
keytool -import -alias <some alias> -file <your certificate>.cer -keystore cacerts