2

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

请帮助我。

这是我的代码:

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 它把我的电脑名放在用户名后面!!我不知道为什么。另外,如何附加文件?请帮我。提前致谢!

4

5 回答 5

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
   }
}
于 2013-08-21T15:18:45.483 回答
3

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

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

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

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

另请参阅JavaMail api上的示例

于 2013-08-21T15:16:43.417 回答
2

使用该setFrom方法也可以设置发件人。

于 2013-08-21T15:10:33.310 回答
2

您的错误消息:发件人被拒绝 - MAIL FROM必须是有效域。

尝试设置 From 字段:MimeMessage.setFrom(Address ...)

于 2013-08-21T15:15:35.240 回答
0

您应该包括“来自”部分:

generateMailMessage.setFrom(new InternetAddress("admin@example.com", "Example.com Admin"));

略低于generateMailMessage = new MimeMessage(getMailSession);

于 2013-08-21T15:06:06.257 回答