gpt4 book ai didi

Java发送电子邮件,获取错误

转载 作者:行者123 更新时间:2023-12-04 21:57:31 27 4
gpt4 key购买 nike

我写了一个发送电子邮件的程序,但我不知道为什么会收到错误。

请帮帮我。

这是我的代码:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class EmailTest {

static Properties mailServerProperties;
static Session getMailSession;
static MimeMessage generateMailMessage;

public static void main(String args[]) throws AddressException, MessagingException {
generateAndSendEmail();
System.out.println("\n\n ===> Your Java Program has just sent an Email successfully. Check your email..");
}

public static void generateAndSendEmail() throws AddressException, MessagingException {


System.out.println("\n 1st ===> setup Mail Server Properties..");
mailServerProperties = System.getProperties();
mailServerProperties.put("mail.smtps.host", "smtpout.secureserver.net");
mailServerProperties.put("mail.smtp.auth", "true");

System.out.println("Mail Server Properties have been setup successfully..");


System.out.println("\n\n 2nd ===> get Mail Session..");
getMailSession = Session.getDefaultInstance(mailServerProperties, null);
generateMailMessage = new MimeMessage(getMailSession);
generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("to@to.com"));
generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("cc@cc.com"));
generateMailMessage.setSubject("TEST");
String emailBody = "TEST BODY" + "<br><br> DFKSDL, <br>JDSKJFDS";
generateMailMessage.setContent(emailBody, "text/html");
System.out.println("Mail Session has been created successfully..");


System.out.println("\n\n 3rd ===> Get Session and Send mail");
Transport transport = getMailSession.getTransport("smtp");
// Enter your correct gmail UserID and Password
transport.connect("smtpout.secureserver.net", "username@user.com", "password");
transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
transport.close();
}
}

我遇到了一个错误。用户名和密码以及一切都正确,但我收到此错误:

 1st ===> setup Mail Server Properties..
Mail Server Properties have been setup successfully..


2nd ===> get Mail Session..
Mail Session has been created successfully..


3rd ===> Get Session and Send mail
Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 550 <username@partik-pc> Sender Rejected - MAIL FROM must be a valid domain.
;
nested exception is:
com.sun.mail.smtp.SMTPSenderFailedException: 550 <username@partik-pc> Sender Rejected - MAIL FROM must be a valid domain.

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
at EmailTest.generateAndSendEmail(EmailTest.java:50)
at EmailTest.main(EmailTest.java:20)
Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 550 <username@partik-pc> Sender Rejected - MAIL FROM must be a valid domain.

at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1616)
... 3 more

我不知道为什么,username@partik-pc 它将我的 PC 名称放在用户名后面!!我不知道为什么。另外,如何附加文件?请帮我。提前致谢!

最佳答案

您需要使用邮件服务器接受的有效电子邮件地址添加 mail.from 属性:

mailServerProperties.put("mail.from", "your@emailaddress.com");

或者您需要明确设置发件人地址:

generateMailMessage.setFrom("me@example.com");

另请参阅 api of JavaMail 上的示例

关于Java发送电子邮件,获取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18361029/

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