我正在用java构建一个电子邮件应用程序,到目前为止我尝试使用它,我在运行此代码时没有错误,但编译和运行代码后的问题是net beans IDE只显示了我,程序正在运行,但是当我检查邮箱时,没有收到邮件。任何人都可以解释为什么会发生这种情况?
此外,我在最后打印了一条消息“已成功发送”消息,但不知何故消息未被打印,我无法弄清楚错误是什么,任何帮助都将不胜感激,谢谢。
public class Email
public static void main(String[] args) {
// TODO code application logic here
String[] to = {"pqr@gmail.com"};
String from = "abc@gmail.com";
String host = "smtp.gmail.com";
String user_name = "abc@gmail.com";
String password = "xyz";
try{
Properties properties = System.getProperties();
properties.setProperty("smtp.gmail.com", "imaps");
properties.put("smtp.starttls.enable", "true");
properties.put("mail.smtp.host",host);
properties.put("mail.smtp.user",user_name);
properties.put("mail.smtp.password", password);
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.auth", "true");
//properties.put
Session session = Session.getDefaultInstance(properties,null);
//Store store = session.getStore("imaps");
MimeMessage message = new MimeMessage(session);
// basically stores one or more addresses , whom the mail has to be sent
//InternetAddress[] send_to = { new InternetAddress(to) };
//InternetAddress[] send_from = { new InternetAddress(from) };
InternetAddress[] toAddress = new InternetAddress[to.length];
for(int i = 0; i < to.length ;i++)
{
toAddress[i] = new InternetAddress(to[i]);
}
for(int i = 0 ; i < toAddress.length ; i++){
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setFrom(new InternetAddress(from));
message.setSubject("Testing email");
message.setText("this is a test");
Transport transport = session.getTransport("smtp");
transport.connect(host, user_name, password);
transport.send(message);
transport.close();
System.out.println("Message successfully sent");
}catch(Exception e){
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
试试此代码
public class SendMail {
String host, port, emailid,username, password;
Properties props = System.getProperties();
Session l_session = null;
public SendMail() {
host = "smtp.mail.yahoo.com";
port = "587";
emailid = "a@yahoo.com";
username = "a";
password = "pwd";
emailSettings();
createSession();
sendMessage("a@yahoo.com", "rahul@gmail.com","Test","test Mail");
}
public void emailSettings() {
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "false");
props.put("mail.smtp.port", port);
// props.put("mail.smtp.socketFactory.port", port);
// props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// props.put("mail.smtp.socketFactory.fallback", "false");
}
public void createSession() {
l_session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
l_session.setDebug(true); // Enable the debug mode
}
public boolean sendMessage(String emailFromUser, String toEmail, String subject, String msg) {
//System.out.println("Inside sendMessage 2 :: >> ");
try {
//System.out.println("Sending Message *********************************** ");
MimeMessage message = new MimeMessage(l_session);
emailid = emailFromUser;
//System.out.println("mail id in property ============= >>>>>>>>>>>>>> " + emailid);
//message.setFrom(new InternetAddress(emailid));
message.setFrom(new InternetAddress(this.emailid));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(AppConstants.fromEmail));
message.setSubject(subject);
message.setContent(msg, "text/html");
//message.setText(msg);
Transport.send(message);
System.out.println("Message Sent");
} catch (MessagingException mex) {
mex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}//end catch block
return true;
}
}
答案 1 :(得分:1)
如果您在代理/防火墙后面运行,则可能需要设置代理
System.setProperty("http.proxyHost", "myProxyServer.com");
System.setProperty("http.proxyPort", "80");
答案 2 :(得分:0)
谢谢大家,看起来我错过了
props.put(&#34; mail.smtp.socketFactory.port&#34;,&#34; 587&#34;);
部分,添加了这个和它的工作正常。