gpt4 book ai didi

Java发送邮件失败,使用SMTP的gmail

转载 作者:行者123 更新时间:2023-11-29 05:44:02 25 4
gpt4 key购买 nike

我已经编写了用于发送邮件的 Java 代码,当我使用端口号 587 时出现异常,而当我使用端口号 465 时仍在运行而不会停止。我用的是tomcat,不知道有没有配置。邮件.java

public class Mail {
String to;
String from;
String message;
String subject;
String smtpServ;
//getter and setter
public int sendMail(){
try
{
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp" );
props.put("mail.smtp.starttls.enable","true" );
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.auth", "true" );
props.put("mail.smtp.port", "456");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to,false));
msg.setSubject(subject);
msg.setText(message);
msg.setHeader("MyMail", "Mr. XYZ" );
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Message sent to"+to+" OK." );
return 0;
}
catch (Exception ex)
{
ex.printStackTrace();
System.out.println("Exception "+ex);
return -1;
}
}
}


public class SMTPAuthenticator extends javax.mail.Authenticator {
@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = "***@gmail.com"; // specify your email id here (sender's email id)
String password = "***"; // specify your password here
return new PasswordAuthentication(username, password);
}
}

index.jsp:

<body>
<form action="sendMail.jsp" method="POST">
<input type="text" name="to" value="" />
<input type="text" name="subject" value="" />
<textarea name="message" rows="8" cols="30">
</textarea>
<input type="submit" value="Send Mail" />

发送邮件.jsp

<jsp:useBean id="mail" scope="session" class="mailing.Mail" />
<jsp:setProperty name="mail" property="to" param="to" />
<jsp:setProperty name="mail" property="from" value="****@gmail.com" />
<jsp:setProperty name="mail" property="smtpServ" value="smtp.gmail.com" />
<jsp:setProperty name="mail" property="subject" param="subject" />
<jsp:setProperty name="mail" property="message" param="message" />
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
...
<body>
<%
String to = mail.getTo();
int result;
result = mail.sendMail();
}
%>

最佳答案

这段代码工作正常。试试这个

public static void sendEmail(Email mail) {

String host = "smtp.gmail.com";
String from = "YOUR_GMAIL_ID";
String pass = "YOUR_GMAIL_PASSWORD";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");

// Get the default Session object.
Session session = Session.getDefaultInstance(props, null);

try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);

// Set sender
message.setFrom(new InternetAddress("Senders_EMail_Id"));

// Set recipient
message.addRecipient(Message.RecipientType.TO, new InternetAddress("RECIPIENT_EMAIL_ID"));

// Set Subject: header field
message.setSubject("SUBJECT");

// set content and define type
message.setContent("CONTENT", "text/html; charset=utf-8");

Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException mex) {
System.out.println(mex.getLocalizedMessage());
}

关于Java发送邮件失败,使用SMTP的gmail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16441005/

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