我使用以下java程序从gmail帐户发送邮件
final String username = "user@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", "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("user@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("user@live.in"));
message.setSubject("Testing Subject");
message.setText("Dear Bhavik Patel," +
"\n\n This is just a mail!");
Transport.send(message);
System.out.println("Done");
} catch (Exception e) {
throw new RuntimeException(e);
}
我也尝试过端口587,但它无法正常工作
Transport.send(message);
在执行时尝试连接并发送
我不知道它有什么问题。我也试过telnet,从那里我可以连接
例外:
java.lang.RuntimeException:javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,port:465,响应:-1
答案 0 :(得分:0)
我使用端口587
,我可以发送/接收邮件..或者您可以设置属性
mail.smtp.timeout 到 25000 并尝试是否为超时例外..
答案 1 :(得分:0)
试试这些JavaMail FAQ条目:
您需要设置“mail.smtp.ssl.enable”,而不是starttls。
答案 2 :(得分:0)
我的send_mail
代码与您的代码之间存在细微差别,但我无法确定您的问题是由什么导致的。这是我的代码:
public static int sendMail(String SMTPServer,
String Sender,
String Recipient,
String Subject,
String Body,
String ErrorMessage,
String Attachments) {
// Error status;
int ErrorStatus = 0;
// Create some properties and get the default Session;
final String username = Sender;
final String password = "passwd";
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 {
// Create a message.
MimeMessage msg = new MimeMessage(session);
// extracts the senders and adds them to the message.
// Sender is a comma-separated list of e-mail addresses as per RFC822.
{
InternetAddress[] TheAddresses = InternetAddress.parse(Sender);
msg.addFrom(TheAddresses);
}
// Extract the recipients and assign them to the message.
// Recipient is a comma-separated list of e-mail addresses as per RFC822.
{
InternetAddress[] TheAddresses = InternetAddress.parse(Recipient);
msg.addRecipients(Message.RecipientType.TO,TheAddresses);
}
// Subject field
msg.setSubject(Subject);
// Create the Multipart to be added the parts to
Multipart mp = new MimeMultipart();
// Create and fill the first message part
{
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(Body);
// Attach the part to the multipart;
mp.addBodyPart(mbp);
}
// Add the Multipart to the message
msg.setContent(mp);
// Set the Date: header
msg.setSentDate(new Date());
// Send the message;
Transport.send(msg);
} catch (MessagingException MsgException) {
System.out.println("blows here");
ErrorMessage = MsgException.toString();
Exception TheException = null;
if ((TheException = MsgException.getNextException()) != null)
ErrorMessage += "\n" + TheException.toString();
ErrorStatus = 1;
}
System.out.println(ErrorMessage);
return ErrorStatus;
}
我一直在使用这段代码而没有任何问题。
希望它有所帮助,