gpt4 book ai didi

java - 在我的代码中实现电子邮件服务

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:48 28 4
gpt4 key购买 nike

我一直在尝试实现以下 servlet 代码来实现电子邮件功能,但我不断收到此代码的错误。请看一下...

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.RequestDispatcher;

public class email extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String to="amanmaheshwari25@gmail.com"; //emailID at which the mail is send

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Session session=null;

try{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("cconatus.2011@gmail.com","password");//change accordingly
}
});}
catch(Exception e)
{out.println("error234");}
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("ccnatus.2011@gmail.com"));//change accordingly
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Pls find the message inside");
String dd = "hey";
message.setText(dd);

//send message
Transport.send(message);
out.println("success");
}
catch(Exception e)
{out.println("error");}
finally {
out.close();
}
}

我得到的输出是:错误,即来自最后一个 catch block 的错误请尽快就此提出一些建议。

最佳答案

以下是对我有用的属性:

mail.transport.protocol = smtp

mail.smtp.host = smtp.gmail.com

mail.smtp.auth = true

mail.smtp.starttls.enable = true

mail.smtp.port = 587

关于java - 在我的代码中实现电子邮件服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15296272/

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