我写了一个程序来发送电子邮件,但我不知道为什么会出错。
请帮助我。
这是我的代码:
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 它把我的电脑名放在用户名后面!!我不知道为什么。另外,如何附加文件?请帮我。提前致谢!