gpt4 book ai didi

email - 用java发送电子邮件

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

我正在创建一个应用程序,我想在其中向我的客户发送电子邮件。当我编译下面的代码时,它没问题,但是当我运行时,它给出了如下错误

java code:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail
{
public static void main(String [] args)
{

String to = "prakash_d22@rediffmail.com";
String from = "web@gmail.com";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("smtp.gmail.com", host);
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("This is the Subject Line!");
message.setText("This is actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}

错误: enter image description here

请指导我。

最佳答案

            String host = "smtp.gmail.com";

Properties properties = new Properties();

设置以下属性

        properties .put("mail.smtp.starttls.enable", "true");
properties .put("mail.smtp.host", host);
properties .put("mail.smtp.user", username);
properties .put("mail.smtp.password", password);
properties .put("mail.smtp.port", "587");
properties .put("mail.smtp.auth", "true");

关于email - 用java发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11999935/

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