Mail

xiaoxiao2021-03-06  74

Package yaoyao.classes;

Import javax.mail. *; public class mailauthenticator extends automator {private string name = null; // username private string password = null; // user password

public MailAuthenticator () {} public MailAuthenticator (String name, String password) {this.name = name; this.password = password;} public void setName (String name) {this.name = name;} public void setPassword (String password ) {This.password = password; public passwordAuthentication getPasswordAuthentication () {return new passwordAuthentication (name, password);}}

-------------------------------------------------- ------------

Package yaoyao.classes;

Import java.io. *; import javax.activation. *;

Public class stringDataSource Implements javax.activation.datasource {private string data; private string type;

Public StringDataSource (String Data, String Type) {this.data = data; this.type = type;}

public InputStream getInputStream () throws IOException {if (data == null) {throw new IOException ( "Data is null");} return new ByteArrayInputStream (data.getBytes ()); // return new StringBufferInputStream (new String (data. getBytes ( "iso-8859-1"), "GB2312"));} public OutputStream getOutputStream () throws IOException {throw new IOException ( "Data is readonly");} public String getContentType () {return type;} public String GetName () {Return "stringDataSource";}}

-------------------------------------------------- ------------

Package yaoyao.classes;

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

Import yaoyao.classes. *;

public class ReMail {String smtpHost = "smtp.sohu.com"; String mailFrom = "gelikeji@sohu.com"; String mailUser = "gelikeji"; String mailPassword = "44348214"; String mailTo = null; String mailSubject = null; String mailContent = null; public void setMailTo (String mailTo) {this.mailTo = mailTo;} public void setMailSubject (String mailSubject) {this.mailSubject = mailSubject;} public void setMailContent (String mailContent) {this.mailContent = mailContent;} Public string getMailto () {return getMailsubject () {return mailsubject;} public string getmailcontent () {return mailcontent;}

Public void send () {Try {property pROPS = system.getproperties (); Props.Put ("mail.smtp.host", smtphost); prOPs.Put ("mail.smtp.auth", "true"); Authenticator auth = new MailAuthenticator (mailUser, mailPassword); Session mailSession = Session.getDefaultInstance (props, auth); Message msg = new MimeMessage (mailSession); msg.setFrom (new InternetAddress (mailFrom)); msg.setRecipients (Message.RecipientType. TO, InternetAddress.parse (getMailTo ())); msg.setSubject (getMailSubject ()); msg.setDataHandler (new DataHandler (new StringDataSource (getMailContent (), "text / html"))); Transport.send (msg) ;} Catch (exception e) {}}}

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

New Post(0)