Sendmail

xiaoxiao2021-03-06  101

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

Public class sendmail {

Private string host; private string user; private string password;

Public sendmail () {}

public void send (String to, String subject, String body) throws Exception {Properties props = new Properties (); Session sendMailSession; Transport transport; props.put ( "mail.smtp.auth", "true"); props.put ("Mail.smtp.host", Host); // SMTP host name. Props.Put ("mail.smtp.user", user); // senders email address. Props.put ("mail.smtp.password", password); // mail password. Popupauthenticator popa = new popupauthenticator (); // message security authentication. int index = user.indexOf ( "@"); String userName = user.substring (0, index); PasswordAuthentication pop = popA.performCheck (userName, password); // fill in your username and password sendMailSession = Session.getInstance (props , popA); message newMessage = new MimeMessage (sendMailSession); newMessage.setFrom (new InternetAddress (user)); newMessage.setRecipient (Message.RecipientType.TO, new InternetAddress (to)); // recipient mail address newMessage.setSubject (newmed); newmed ()); newMessage.Settext (body); // message body transport = sendmailsession.getTransport ("smtp"); transport.send (newMessage);}

Public string gethost () {return host;}

Public void sethost (string host) {this.host = host;}

Public string getuser () {return user;}

Public void setuser (String user) {this.user = user;}

Public string getpassword () {return password;}

public void setPassword (String password) {this.password = password;} 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);

}

转载请注明原文地址:https://www.9cbs.com/read-97564.html

New Post(0)