| |

VerySource

 Forgot password?
 Register
Search
View: 880|Reply: 2

javaMail problem

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-4 13:00:01
| Show all posts |Read mode
I wrote according to an example on the Internet, and gave an error
I found nothing wrong for a day,

package com;

import java.io. *;
import java.util. *;
import javax.mail. *;
import javax.mail.internet. *;



class ExtendString {
public ExtendString () {
}

/ **
* Remove the white space characters at the ends of the string and convert the string to a Chinese standard character gb2312.
* /
public String CS (String str) {// remove white space characters at the end of the string
try {
if (str == null)
return "";
str = str.trim ();
if (str == null)
return "";
str = new String (str.getBytes ("8859_1"), "GBK");
} catch (Exception e) {
System.out.println (e);
}
return str;
}

}

public class SendMail {
private String errMsg = "";

private ExtendString ExStr = new ExtendString ();

private String sender = ""; // sender address

private String smtpHost = ""; // mail sending server (smtp)

private String user = ""; // login user name

private String password = ""; // login password

private String subject = ""; // mail subject

public SendMail () {
this.setPropertiesAttri ();
}

private void setPropertiesAttri () {
try {
/ * InputStream is = getClass (). GetResourceAsStream (
"MailServer.properties");
Properties prop = new Properties ();
prop.load (is);

this.setSmtpHost (prop.get ("SmtpHost"). toString ());
this.setUser (prop.get ("User"). toString ());
this.setPassword (prop.get ("Password"). toString ());
this.setSender (prop.get ("Sender"). toString ());
this.setSubject (ExStr.CS (prop.get ("Subject"). toString ()));
* /
this.setSmtpHost ("smtp.163.com");
this.setUser ("mysky.15");
this.setPassword ("hibernate");
this.setSender ("mysky.15@163.com");
this.setSubject ("content");

} catch (Exception ex) {
System.err.println ("ex1 in sendmail.java:" + ex.toString ());
}
}

/ ** Set sender address * /

public void setSender (String sender) {
this.sender = sender;
}

public String getSender () {
return sender;
}

/ ** Set up mail sending server (smtp) * /
public void setSmtpHost (String smtpHost) {
this.smtpHost = smtpHost;
}

public String getSmtpHost () {
return smtpHost;
}

/ ** Set login username * /
public void setUser (String user) {
this.user = user;
}

public String getUser () {
return user;
}

        /** Setting login password */
public void setPassword (String password) {
this.password = password;
}

public String getPassword () {
return password;
}

/ ** Set mail subject * /
public void setSubject (String subject) {
this.subject = subject;
}

public String getSubject () {
return subject;
}

/ **
* Send mail using smtp Main program
*
* @throws MessagingException
* mail sending failed
* /
public void smtp (String receiver, String content) throws MessagingException {
if (smtpHost == null)
throw new MessagingException ("smtpHost not found");
if (user == null)
throw new MessagingException ("user not found");
if (password == null)
throw new MessagingException ("password not found");

Properties properties = new Properties ();
properties.put ("mail.smtp.host", smtpHost); // Set the smtp host
properties.put ("mail.smtp.auth", "true"); // Use smtp authentication

Session session = Session.getDefaultInstance (properties,
new Authenticator () {
public PasswordAuthentication getPasswordAuthentication () {
return new PasswordAuthentication (user, password);
}
});

// get mail conversation object
MimeMessage mimeMsg = new MimeMessage (session); // Create a MIME mail object
if (sender! = null) // Set sender address
{
mimeMsg.setFrom (new InternetAddress (sender));
}
if (receiver! = null) // Set recipient address
{
mimeMsg.setRecipients (Message.RecipientType.TO, parse (receiver));
}
if (subject! = null) // Set email subject
{
mimeMsg.setSubject (subject, "GBK");
}
MimeBodyPart part = new MimeBodyPart (); // mail content part
part.setText (content == null? "": content, "GBK");

// Set the mail format to html cqc
part.setContent (content.toString (), "text / html; charset = GBK");
Multipart multipart = new MimeMultipart ();
multipart.addBodyPart (part); // Add mail content part in Multipart
mimeMsg.setContent (multipart); // Add Multipart to the message body
mimeMsg.setSentDate (new Date ()); // Set the send date
Transport.send (mimeMsg); // send mail
}

/ ** Parse the address collection string * /
private InternetAddress [] parse (String addressSet) throws AddressException {
ArrayList list = new ArrayList ();
StringTokenizer tokens = new StringTokenizer (addressSet, ";");
while (tokens.hasMoreTokens ()) {
list.add (new InternetAddress (tokens.nextToken (). trim ()));
}
InternetAddress [] addressArray = new InternetAddress [list.size ()];
list.toArray (addressArray);
return addressArray;
}

/ **
* Interface for external calls
* /

public boolean sendMails (String mail, String content) {
int mailLen = 0;
int contentLen = 0;
if (mail == null || content == null) {
return false;
}

try {
this.smtp (mail, content);
} catch (Exception ex) {
ex.printStackTrace ();
//System.err.println("ex2 in sendmail.java: "+ ex.toString ());
}

return true;
}

public static void main (String [] args) {

SendMail mail = new SendMail ();
String email = "mysky15@vip.winzheng.com";
String content = "I try";
try {
mail.sendMails (email, content);
} catch (Exception ex) {
System.err.println ("ex33:" + ex.toString ());
}
}

}


-------------------------------------------------- ---
Report the following error
javax.mail.SendFailedException: Sending failed;
  nested exception is:
class javax.mail.AuthenticationFailedException
at javax.mail.Transport.send0 (Transport.java:218)
at javax.mail.Transport.send (Transport.java:80)
at com.SendMail.smtp (SendMail.java:185)
at com.SendMail.sendMails (SendMail.java:212)
at com.SendMail.main (SendMail.java:227)
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-5 12:51:01
| Show all posts
It shows that the email verification fails. You have to find what is written in Transport.java:218, SendMail.java:185, what to do, and to find the cause of the error according to the operation performed.
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 Unknown

 Author| Post time: 2020-1-6 10:24:01
| Show all posts
Transport.java is a class under the javax.mail package, not what I wrote
SendMail.java:185 line is mail.sendMails (email, content);
I did not find anything wrong
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