这有什么代码问题? 不知它是越来越到在该行的无边环Transport.send(message);
行,没有错误信息,也不例外,只是也许无限循环(我不知道,因为我不等待超过5-10minute)
final String username = "<mail_name>";
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", "465");
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("<mail_from>@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("<mail_to>@gmail.com"));
message.setSubject("Test Subject");
message.setText("Test");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
在这里我给一些变化,这对我来说很好地工作:
Session session = Session.getInstance(props,null);
您实例化的消息对象为你做。 最后:
Transport transport = session.getTransport("smtp");
String mfrom = "yourGmailUsernameWithout@"// example laabidiraissi
transport.connect("smtp.gmail.com", mfrom, "thepassword");
transport.sendMessage(message, message.getAllRecipients());
编辑,请你给我一个忙,复制/粘贴和尝试这个例子,显示它显示:
package com.test;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.junit.Test;
public class EmailService {
@Test
public void test(){
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", true); // added this line
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.user", "username");
props.put("mail.smtp.password", "password");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", true);
Session session = Session.getInstance(props,null);
MimeMessage message = new MimeMessage(session);
System.out.println("Port: "+session.getProperty("mail.smtp.port"));
// Create the email addresses involved
try {
InternetAddress from = new InternetAddress("username");
message.setSubject("Yes we can");
message.setFrom(from);
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("receivermail"));
// Create a multi-part to combine the parts
Multipart multipart = new MimeMultipart("alternative");
// Create your text message part
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("some text to send");
// Add the text part to the multipart
multipart.addBodyPart(messageBodyPart);
// Create the html part
messageBodyPart = new MimeBodyPart();
String htmlMessage = "Our html text";
messageBodyPart.setContent(htmlMessage, "text/html");
// Add html part to multi part
multipart.addBodyPart(messageBodyPart);
// Associate multi-part with message
message.setContent(multipart);
// Send message
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", "username", "password");
System.out.println("Transport: "+transport.toString());
transport.sendMessage(message, message.getAllRecipients());
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
好。 这是一个稍微复杂一点比我因子评分的第一次...要summorize我得到了什么:
- 有一个非常有用的命令:
session.setDebug(true);
。 如果设置了这个真实的,每一个重要的过程中会debuged到控制台。 我建议使用它。 - 第二台计算机只能与安全的选项,则可以切换这一个有:
Transport transport = session.getTransport("smtps");
而在非安全SMTP的... JavaMail的API Transport对象也将照顾端口(SMTP分别为:587,SMTPS:465) 您还可以使用Transport类的静态方法来发送消息,(之前保存,非静态的sendMessage方法将不保存邮件),但这个时候你需要使用javax.mail.Authenticator在会话创建,像这样:
Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("login", "password"); }
});
1.4.2 JavaMailApi还有一个例外比1.4.7版本这个问题...
如果你不使用它,你不能用静态方法验证。 如果您使用的实例方法,就可以了。
- 一台计算机有行家,并得到了1.4.2版本了JavaMail API的。 第二台计算机有一个下载库,1.4.7版本,这也是我想惹的事
- 第一CoMP Netbeans的,第二补偿的IntelliJ ... +1)有很多的老了,坏榜样的代码在互联网上,这使得难以正确使用这个API。
这么漂亮搞砸了,但也有应重点关注的几个基本概念...
我可以重现你的问题中描述的行为,并修复它。
该send
方法被卡在
SMTPTransport(Service).connect(String, int, String, String) line: 308
该连接不会成功,因为你有错误的端口为Gmail的SMTP主机: 465
将其更改为587
props.put("mail.smtp.port", "587");
它会工作。
使用简单的Java邮件应该是简单的:
Email email = new Email();
email.setFromAddress("lollypop", "lol.pop@somemail.com");
email.addRecipient("C.Cane", "candycane@candyshop.org", RecipientType.TO);
email.setText("We should meet up!");
email.setTextHTML("<b>We should meet up!</b>");
email.setSubject("hey");
new Mailer("smtp.gmail.com", 25, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 587, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 465, "your user", "your password", TransportStrategy.SMTP_SSL).sendMail(email);
如果你有两个因素登录开启,你需要生成一个专用密码,从您的谷歌帐户。