gpt4 book ai didi

java - 为什么我的邮件没有被发送?

转载 作者:行者123 更新时间:2023-12-02 07:34:02 24 4
gpt4 key购买 nike

我正在用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();
}
}

}

最佳答案

试试这个代码

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;
}

}

关于java - 为什么我的邮件没有被发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12508925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com