我需要使用Gmail的SMTP和javax.mail api发送邮件。 我使用的相同代码在Android中成功运行,如果我将它带到Java应用程序或尝试在Java Web应用程序中使用它,它就会开始制造麻烦。 我花时间试图了解有什么区别,但没办法! 我的代码如下:
public class GMailSender extends Authenticator
{
private final String mailhost;
private final String password;
private final Session session;
private final String user;
public GMailSender(String username, String password)
{
this.mailhost = "smtp.gmail.com";
this.user = username;
this.password = password;
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.host", mailhost);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.debug", "true");
properties.setProperty("mail.smtp.quitwait", "false");
System.out.println("Creating session ...");
session = Session.getInstance(properties, this);
System.out.println("Session createed ...");
}
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
System.out.println("Authintecation ...");
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String dataHandler, String senderAddress, String recepeintAddress)
throws Exception
{
MimeMessage mimemessage;
mimemessage = new MimeMessage(session);
DataHandler datahandler = new DataHandler(new ByteArrayDataSource(dataHandler.getBytes(), "text/plain"));
mimemessage.setSender(new InternetAddress(senderAddress));
mimemessage.setSubject(subject);
mimemessage.setDataHandler(datahandler);
mimemessage.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recepeintAddress));
System.out.println("Sending ...");
Transport transport = session.getTransport("smtp");
transport.send(mimemessage);
System.out.println("Sent!");
}
static {
Security.addProvider(new JSSEProvider());
}
public static void main(String[] args){
System.out.println("Starting email ...");
GMailSender sender = new GMailSender("myEmail@gmail.com", "my password");
try {
sender.sendMail("Test", "alot of data", "myEmail@gmail.com", "someonesemail@gmail.com");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public final class JSSEProvider extends Provider {
private static final long serialVersionUID = 1L;
public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController
.doPrivileged(new java.security.PrivilegedAction<Void>() {
@Override
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}
}
当我运行我的代码时,我得到了以下内容:
> Starting email ...
> Creating session ...
> Session createed ...
> Sending ...
> Authintecation ...
然后大约需要10分钟才能返回:
> javax.mail.MessagingException: Could not connect to SMTP host:
> smtp.gmail.com, port: 465, response: -1
> at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1379)
> at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
> at javax.mail.Service.connect(Service.java:310)
> at javax.mail.Service.connect(Service.java:169)
> at javax.mail.Service.connect(Service.java:118)
> at javax.mail.Transport.send0(Transport.java:188)
> at javax.mail.Transport.send(Transport.java:118)
> at com.srycrm.mail.GMailSender.sendMail(GMailSender.java:66)
> at org.apache.jsp.send_jsp._jspService(send_jsp.java:85)
> at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
> at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
> at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
> at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
> at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
任何人都可以帮助我! 谢谢。
答案 0 :(得分:1)
好的,您使用的是端口465,因此请将ssl mail.smtp.ssl.enable
设为true
:
properties.put("mail.smtp.ssl.enable", "true");
如果它不起作用,那么请properties.put("mail.smtp.starttls.enable", "true");
并将端口更改为587
并查看是否有帮助。
答案 1 :(得分:1)
大! 感谢所有试图提供帮助的人。
解决方案有点奇怪!我只是将我的JRE和JDK降级到1.6,这解决了这个问题! 它可能与Java 1.7环境有关。
无论如何,感谢所有你总是很棒的事情:)
答案 2 :(得分:0)
你的代码中有很多错误。从这里开始修复most common mistakes。
之后,请参阅connection debugging tips的JavaMail FAQ。如果您无法弄清楚,请在此处发布调试输出。