| |

VerySource

 Forgot password?
 Register
Search
View: 655|Reply: 3

java email error, help look ~~~~

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-20 06:00:02
| Show all posts |Read mode
I wrote a small class for sending emails according to the book, but I can't always send it out. Please help me check it. There are two classes, MailSedn.java is sent, and s.java is the main class.
//MailSend.java:

import java.io. *;
import java.text. *;
import java.net. *;
import java.util.Properties;
import java.util.Date;
import javax.mail. *;
import javax.mail.internet. *;

public class MailSend {
String subject = null, text = null, mailhost = null;
public MailSend () {
mailhost = "localhost";
text = getUserText ();
subject = "Welcome ~";
}
public String sednMsg (String from, String to) {
boolean debug = false;
String err = null;
try {
Properties props = System.getProperties ();
if (mailhost! = null)
props.put ("mail.smtp.host", mailhost);
Session session = Session.getDefaultInstance (props, null);
if (debug)
session.setDebug (true);
Message msg = new MimeMessage (session);
Ranch
// from = null;
if (from! = null) {
msg.setFrom (new InternetAddress (from));
System.out.print ("from is not null ~");
}
else {
msg.setFrom ();
System.out.print ("from is null ~");
}
msg.setRecipients (Message.RecipientType.TO, InternetAddress.parse (to, false));
Ranch
msg.setSubject (subject);
msg.setText (text);
Ranch
msg.setSentDate (new Date ());
Transport.send (msg);
System.out.println ("Mail has been send successfully!");
Ranch
}
catch (Exception e) {
e.printStackTrace ();
err = e.toString ();
err = "Mail send has an error!";
}
return err;
}
Ranch
public String getUserText () {
String userText = "";
String nowDate = DateFormat.getDateInstance (). Format (new Date ());
userText = "hello ~";
return userText;
}

}


//S.java


public class S {

/ **
* @param args
* /
public static void main (String [] args) {
// TODO Auto-generated method stub
String mail = new String ("suhuajun2001@163.com");
MailSend ms = new MailSend ();
ms.sednMsg ("shj0717@gmail.com", "suhuajun2001@163.com");
//ms.sednMsg ("", mail);

}

}


The error message is as follows:
javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for suhuajun2001@163.com

at com.sun.mail.smtp.SMTPTransport.rcptTo (SMTPTransport.java:1196)
at com.sun.mail.smtp.SMTPTransport.sendMessage (SMTPTransport.java:584)
at javax.mail.Transport.send0 (Transport.java:169)
at javax.mail.Transport.send (Transport.java:98)
at server.MailSend.sednMsg (MailSend.java:47)
at server.S.main (S.java:12)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for suhuajun2001@163.com

Let's see what is the reason. Also, I have included the jar files (mail.java, activation.java) to be used ~~~
Reply

Use magic Report

0

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-29 05:45:02
| Show all posts
You should set your password to send it, mailhost is not right, right?
Reply

Use magic Report

0

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-29 10:18:01
| Show all posts
/ *
 *
 * TODO To change the template for this generated file, go to
 * Window-Preferences-Java-Code Style-Code Template
 * /
package com.day1124;

/ **
 * @author dengjiang
 *
 * TODO To change the template of this generated type annotation, go to
 * Window-Preferences-Java-Code Style-Code Template
 * /
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendMail {

    public SendMail () {
    }

    public void send () {
        try {
            Properties props = new Properties ();
            Session sendMailSession;
            Store store;
            Transport transport;
            props.put ("mail.smtp.auth", "true");
            props.put ("mail.smtp.host", "smtp.163.com"); // smtp host name.
            props.put ("mail.smtp.user", "test@163.com"); // Sender's email address.
            props.put ("mail.smtp.password", "111111"); // Mail password.
            PopupAuthenticator popA = new PopupAuthenticator (); // Email security authentication.
            PasswordAuthentication pop = popA.performCheck ("test", "11111"); // fill in username and password
            sendMailSession = Session.getInstance (props, popA);
            Message newMessage = new MimeMessage (sendMailSession);
            newMessage.setFrom (new InternetAddress ("test@163.com"));
            newMessage.setRecipient (Message.RecipientType.TO,
                    new InternetAddress ("luozonghang@163.com")); // Recipient's email address
            String subject = "message subject";
            String tmp = new String (subject.getBytes ("GBK"));
            newMessage.setSubject (tmp);
            newMessage.setSentDate (new Date ());
            String mailContent;
            mailContent = "Hello hellow";
            mailContent + = "\tmail body content\n\n";
            String content1 = "hrrhrhrhrhhhhhhhhhhhhhhhhhhhhhhhh";
            Ranch
           mailContent + = new Date (). toLocaleString ();
            newMessage.setText (new String (content1.getBytes ("big5"))); // Message body
            transport = sendMailSession.getTransport ("smtp");
            transport.send (newMessage);
        } catch (MessagingException ex) {
            ex.printStackTrace ();
        } catch (UnsupportedEncodingException e) {
            // TODO automatically generates a catch block
            e.printStackTrace ();
        }
    }

    public static void main (String [] args) {
        SendMail sml = new SendMail ();
        sml.send ();
    }

    public class PopupAuthenticator extends Authenticator {
        String username = null;

        String password = null;

        public PopupAuthenticator () {
        }

        public PasswordAuthentication performCheck (String user, String pass) {
            username = user;
            password = pass;
            return getPasswordAuthentication ();
        }

        protected PasswordAuthentication getPasswordAuthentication () {
            return new PasswordAuthentication (username, password);
        }
    }
}


Change your email address to your email address.
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-3-10 19:30:02
| Show all posts
The problem is solved, thank you very much ~~~~~
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list