由于Stack Overflow不允许我回答我的问题,我将在此处发布解决方案:Google SMTP Server在某些国家/地区不起作用,请尝试使用类似TunelBear的VPN,然后重试。它对我有用。
我正在尝试从我的应用程序向用户电子邮件发送验证码。
问题在于,除了不发送电子邮件之外,应用程序还会继续运行而不会抛出任何错误或给出任何响应。
PS。我在我的Google帐户上启用了“安全性较低的应用”。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* https://www.google.com/settings/security/lesssecureapps
*/
package SendEmail;
/**
*
* @author Naveen
*/
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage;
public class EmailSend {
public static void main(String args[]){
try{
String host ="smtp.gmail.com" ;
String user = "myemail@gmail.com";
String pass = "mypassword";
String to = "emailToSend@gmail.com";
String from = "myemail@gmail.com";
String subject = "This is confirmation number for your expertprogramming account. Please insert this number to activate your account.";
String messageText = "Your Is Test Email :";
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.required", "true");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Session mailSession = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("myEmail@gmail.com",
"myPassword");
} } );
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject); msg.setSentDate(new Date());
msg.setText(messageText);
Transport transport=mailSession.getTransport("smtp");
transport.connect(host, user, pass);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println("message send successfully");
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}
答案 0 :(得分:0)
转到您的Google帐户安全设置,然后执行以下操作:允许安全级别较低的应用程序:启用