Java Mail Example

Source: Internet
Author: User
Tags auth mail mail example

A lot of online, but generally there is no SMTP authentication, below is a sample code:
Cannot run directly, however, you can look inside the section on validation.

Send mail function
public boolean sendMail (String mailto,string mailsubject,string mailbody) {
Send email
try {
Default account information
String smtpserver = "smtp.smtpserver.com";
String Smtpauth = "true";
String Smtpuser = "username";
String Smtppassword = "password";
String from = "from@yourserver.com";
String to = MailTo;
String Subject = Mailsubject;
String Text = mailbody;
Java.util.ResourceBundle Resbundle;
Resbundle = Java.util.ResourceBundle.getBundle ("Mailinfo",
Locale.simplified_chinese);
if (resbundle!= null) {
SmtpServer = resbundle.getstring ("Mail.smtp.host");
Smtpauth = resbundle.getstring ("Mail.smtp.auth");
Smtpuser = resbundle.getstring ("Mail.smtp.user");
Smtppassword = resbundle.getstring ("Mail.smtp.password");
From = resbundle.getstring ("Mail.smtp.from");
}
Properties Props = new properties ();
Session sendmailsession;
Transport transport;
Props.put ("Mail.smtp.host", smtpserver);
Props.put ("Mail.smtp.auth", Smtpauth);
if ("true". Equals (Smtpauth)) {
SMTP server needs authentication, Myauthertiactor to create mail session
Myauthenticator Myauth = new Myauthenticator (Smtpuser, Smtppassword);
Sendmailsession = session.getinstance (props, Myauth);
}
else {
Sendmailsession = session.getinstance (props);
}
Debug
Sendmailsession.setdebug (TRUE);
Message Newmessage = new MimeMessage (sendmailsession);
Newmessage.setfrom (New InternetAddress (from));
Newmessage.setrecipient (Message.RecipientType.TO,
New InternetAddress (MailTo));
Newmessage.setsubject (Subject);
Newmessage.setsentdate (New Date ());
Newmessage.settext (Text);
Newmessage.savechanges ();
Transport = Sendmailsession.gettransport ("SMTP");
Transport.send (Newmessage, newmessage.getallrecipients ());
Transport.close ();
}
catch (Exception Mailex) {
System.err.println ("Send Mail Error:" + mailex.getmessage ());
return false;
}
return true;
}

Authentication class for SMTP to validate
Class Myauthenticator
Extends Javax.mail.Authenticator {
Private String struser;
Private String strpwd;
Public Myauthenticator (string user, string password) {
This.struser = user;
this.strpwd = password;
}

Protected Passwordauthentication getpasswordauthentication () {
return new Passwordauthentication (struser, strpwd);
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.