通过gmail发送电子邮件的属性 [英] Properties for Sending email via gmail
本文介绍了通过gmail发送电子邮件的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在编写一个简单的程序,以通过gmail向gmail帐户发送电子邮件. 尝试了各种方法,但通常我最终遇到相同的错误,无法连接到SMTP主机:smtp.gmail.com,端口:587;"
I'm currently inline of writing a simple program to send email via gmail to a gmail account. Have tried various ways but often i'm ending up on the same error, "Could not connect to SMTP host: smtp.gmail.com, port: 587;"
是否与属性设置有关.这是我程序中的片段.寻找解决方案:)
Whether it has got anything to do with properties settings. Here is the snippet from my program. Looking for a solution :)
预先感谢
public static boolean SendMail(String from, String password, String message, String to[]){
String host = "smtp.gmail.com";
Properties prop = System.getProperties();
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", host);
prop.put("mail.smtp.user", from);
prop.put("mail.smtp.password", password);
prop.put("mail.smtp.port", 587); //prop.put("mail.smtp.port", 465);//
prop.put("mail.smtp.auth", "true");
// check for the first value in the name of props or prop
Session session = Session.getDefaultInstance(prop, null);
MimeMessage mimemsg = new MimeMessage(session);
try{
mimemsg.setFrom(new InternetAddress(from));
// Get reciepents Address
InternetAddress[] toAddress = new InternetAddress[to.length];
for (int i=0;i<to.length;i++){
toAddress[i] = new InternetAddress(to[i]);
}
//Add all toAddress to mimemessage
for(int j=0;j<toAddress.length;j++){
mimemsg.addRecipient(RecipientType.TO, toAddress[j]);
}
// Add Subject.
mimemsg.setSubject(" MAIL from JAVA Program");
// Add Message to the content(input to the method )
mimemsg.setText(message);
Transport trans = session.getTransport("smtp");
trans.connect(host,from,password);
trans.sendMessage(mimemsg, mimemsg.getAllRecipients());
trans.close();
return true;
}catch(MessagingException me){
me.printStackTrace();
}
return false;
}
推荐答案
您的设置似乎正确.对于TLS,GMail SMTP端口为 587 .这看起来像是网络连接问题(主机名无法解决).
Your settings seem correct. GMail SMTP port is 587 for TLS. This looks like a network connectivity issue (the hostname does not resolve).
- 再次检查您的登录凭据.
- 尝试从同一框中输入
telnet smtp.gmail.com 587
.您收到SMTP问候了吗? - 尝试直接使用IP地址. 警告:除了故障排除之外,您不应使用直接IP.它随时可能发生变化,并且使您的代码无用.
- Double check your login credentials.
- Try to
telnet smtp.gmail.com 587
from the same box. Are you getting the SMTP greeting? - Try to directly use an IP address. Caution: you should not use a direct IP except for troubleshooting. It is bound to change at any time and render your code useless.
这篇关于通过gmail发送电子邮件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文