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);
}
}