我编写了以下两个java类 -
public class EmailUtil {
public static void sendEmail(Session session, String toEmail, String subject, String body){
try
{
MimeMessage msg = new MimeMessage(session);
//set message headers
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress("no_reply@no_reply.com", "NoReply-NP"));
msg.setReplyTo(InternetAddress.parse("no_reply@no_reply.com", false));
msg.setSubject(subject, "UTF-8");
msg.setText(body, "UTF-8");
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
System.out.println("Message is ready");
Transport.send(msg);
System.out.println("EMail Sent Successfully!!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public class TLSEmail {
public static void main(String[] args) {
final String fromEmail = "*****@gmail.com";
final String password = "*****";
final String toEmail = "****@gmail.com";
System.out.println("TLSEmail Start");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
props.put("mail.smtp.port", "587"); //TLS Port
props.put("mail.smtp.auth", "true"); //enable authentication
props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
//create Authenticator object to pass in Session.getInstance argument
Authenticator auth = new Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, password);
}
};
Session session = Session.getInstance(props, auth);
EmailUtil.sendEmail(session, toEmail,"TLSEmail Testing Subject", "TLSEmail Testing Body");
}
}
当我运行时,我收到以下错误 -
javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,port:587; 嵌套异常是: java.net.SocketException:Permission denied:connect
我该如何解决这个问题?请帮忙! PS - 我没有使用两步验证 提前谢谢!
答案 0 :(得分:2)
为了让它成功,我有:
我希望这有助于其他人。
答案 1 :(得分:0)
原来,我没有为我的Gmail帐户启用POP / IMAP。现在,一切正常!
其他信息 - 尝试使用外部应用程序连接到Gmail时,请使用下面提到的指南进行成功连接 -
验证您的设置是否正确: a)服务器是smtp.gmail.com或smtp.googlemail.com b)启用SSL或TSL c)传出端口是465,587或25 d)启用传出服务器身份验证
检查防病毒是否在干扰。禁用对外发邮件的防病毒检查。
检查您的ISP是否阻止Gmail。您可能需要使用ISP SMTP服务器。
在您的Gmail帐户中,转到设置并启用POP / IMAP。
注意 - 在运行应用"ping"
smtp.gmail.com
之前,请检查您的计算机是否能够连接到gmail服务器。此外,使用命令行运行&#34; telnet smtp.gmail.com <port number>"
(端口号可以是465,587或25)来检查您的计算机是否能够通过该端口进行访问。
答案 2 :(得分:0)
执行以下步骤:
代码
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class email_try {
public static void main(String ap[]) {
String myEmail = "YOUR EMAIL";
String password = "YOUR PASSWORD";
String opponentEmail = "THEIR EMAIL";
Properties pro = new Properties();
pro.put("mail.smtp.host", "smtp.gmail.com");
pro.put("mail.smtp.starttls.enable", "true");
pro.put("mail.smtp.auth", "true");
pro.put("mail.smtp.port", "587");
Session ss = Session.getInstance(pro, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(myEmail, password);
}
});
try {
Message msg = new MimeMessage(ss);
msg.setFrom(new InternetAddress(myEmail));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(opponentEmail));
msg.setSubject("Your Wish");
msg.setText("java email app");
Transport trans = ss.getTransport("smtp");
Transport.send(msg);
System.out.println("message sent");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
尝试此代码并输入正确的电子邮件ID和密码