我写了一个发送电子邮件的程序,但我不知道为什么会收到错误。
请帮助我。
这是我的代码:
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
我不知道为什么,用户名@ partik-pc将我的PC名称放在用户名后面!!我不知道为什么。另外,我如何附加文件?请帮我。提前致谢!
答案 0 :(得分:3)
您需要使用邮件服务器接受的有效电子邮件地址添加mail.from
媒体资源:
mailServerProperties.put("mail.from", "your@emailaddress.com");
或者您需要明确设置发件人地址:
generateMailMessage.setFrom("me@example.com");
另请参阅api of JavaMail
上的示例答案 1 :(得分:3)
试试这段代码:
public void sendMessageToUser(){
String msgTitle = "Title";
String msgBody = "msgBody";
String userEmail = "user@gmail.com";
final String username = "username";
final String password = "password";
Properties prop = new Properties();
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", "smtp.your.post");
prop.put("mail.smtp.port", "your port");
Session session = Session.getInstance(prop, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("FROM ME"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(userEmail));
message.setSubject(msgTitle);
message.setText(msgBody);
Transport.send(message);
} catch (MessagingException ex){
//Error
}
}
答案 2 :(得分:2)
使用setFrom
方法设置发件人。
答案 3 :(得分:2)
您的错误消息:发件人被拒绝 - MAIL FROM 必须是有效的域。
尝试设置“发件人”字段:MimeMessage.setFrom(Address ...)
答案 4 :(得分:0)
您应该包含“from”部分:
generateMailMessage.setFrom(new InternetAddress("admin@example.com", "Example.com Admin"));
低于generateMailMessage = new MimeMessage(getMailSession);