Home > Back-end >  Java web mailutil real email to email
Java web mailutil real email to email

Time:09-19






Why try to run a few tools, didn't receive the email??

CodePudding user response:

Whether to need to adjust some parameters? Idea maven pom. Under the mode of the XML should be configured with a jar package path

CodePudding user response:

Package cn. Itcast. Travel. Util;

The import javax.mail. Mail. *;
The import javax.mail. Mail. Internet. InternetAddress;
The import javax.mail. Mail. Internet. MimeMessage;
import java.util.Properties;

/* *
* email tools
*/
Public final class MailUtils {
Private static final String USER="";//title of the sender, the same email address
Private static final String PASSWORD="";//if it's qq mailbox can make the authorization code, or login password

/* *
*
* @ param to recipient mailbox
* @ param text email body
* @ param title title
*/
/* to verify information email */
Public static Boolean sendMail (String to the String text, the String title) {
Try {
Final Properties props=new Properties ();
Props. The put (" mail. The SMTP auth ", "true");
Props. The put (mail. The SMTP. "the host", "smtp.qq.com");

//the sender account
Props. The put (" mail. The user, the user);
//the sender password
Props. The put (" mail. Password, "password);

//build authorization information, for SMTP authentication
The Authenticator Authenticator=new Authenticator () {
@ Override
Protected PasswordAuthentication getPasswordAuthentication () {
//user name and password
String userName=props. GetProperty (" mail. User ");
String password=props. GetProperty (" mail. "");
Return new PasswordAuthentication (userName, password);
}
};
//using the environment attributes and authorization information, create a mail session
The Session mailSession=Session. GetInstance (props, the authenticator);
//create a mail message
The MimeMessage message=new MimeMessage (mailSession);
//set the sender
String username=props. GetProperty (" mail. User ");
InternetAddress form=new InternetAddress (username);
Message. SetFrom (form);

//set the recipient
InternetAddress toAddress=new InternetAddress (to);
Message. SetRecipient (message) RecipientType) TO the toAddress);

//set the mail title
Message. SetSubject (title);

//set the mail content of
Message. SetContent (text, text/HTML "; charset=UTF-8");
//email
Transport. Send (message);
return true;
} the catch (Exception e) {
e.printStackTrace();
}
Return false;
}

Public static void main (String [] args) throws the Exception {//do test
MailUtils. SendMail (" 13709280046 @163.com ", "hello, this is a test email, don't need to reply,", "test mail");
System. The out. Println (" 123 ");
}
The source code

CodePudding user response:

Should be Transport. Send (message); The wrong
 try {
The Session Session=Session. GetInstance (prop);
//open the Session the debug mode, so you can see the program to send Email the running state of
The session. SetDebug (true);
//2, transport is obtained by the session object
Transport ts=session. GetTransport ();
//3, the use of email user name and password on the email server, send E-mail, the sender need to submit your email user name and password to the SMTP server,
//user name and password are verified before can normal send mail to the recipient,
Ts. Connect (base_host base_from, base_password);
//4, create email
//create email object
The MimeMessage message=new MimeMessage (session);
//indicate the email sender
Message. SetFrom (new InternetAddress (base_from));
//identify the recipient of the email, now the sender and the recipient is the same, that is oneself give oneself to send
Message. SetRecipients (message) RecipientType) TO the new InternetAddress (). The parse (TO));//the recipient
The title of the//email
Message. SetSubject (title);
//email text content
Message. SetContent (text, text/HTML "; charset=UTF-8");

//5, send E-mail
Ts. SendMessage (message, the message getAllRecipients ());
Ts. The close ();

} the catch (Exception e) {
Logger. The error (" mail delivery failure - {} ", um participant etMessage ());
}

CodePudding user response:

The above clearly incorrect information?

CodePudding user response:

reference lbing100789 reply: 3/f
should be Transport. Send (message); The wrong
 try {
The Session Session=Session. GetInstance (prop);
//open the Session the debug mode, so you can see the program to send Email the running state of
The session. SetDebug (true);
//2, transport is obtained by the session object
Transport ts=session. GetTransport ();
//3, the use of email user name and password on the email server, send E-mail, the sender need to submit your email user name and password to the SMTP server,
//user name and password are verified before can normal send mail to the recipient,
Ts. Connect (base_host base_from, base_password);
//4, create email
//create email object
The MimeMessage message=new MimeMessage (session);
//indicate the email sender
Message. SetFrom (new InternetAddress (base_from));
//identify the recipient of the email, now the sender and the recipient is the same, that is oneself give oneself to send
Message. SetRecipients (message) RecipientType) TO the new InternetAddress (). The parse (TO));//the recipient
The title of the//email
Message. SetSubject (title);
//email text content
Message. SetContent (text, text/HTML "; charset=UTF-8");

//5, send E-mail
Ts. SendMessage (message, the message getAllRecipients ());
Ts. The close ();

} the catch (Exception e) {
Logger. The error (" mail delivery failure - {} ", um participant etMessage ());
}
mail client need to open a protocol can be resolved from the local sent
  • Related