この記事では、参照用の添付ファイル付きの電子メールを送信するJava Web STMPの特定のコードを共有しています。特定のコンテンツは次のとおりです
public class mailfilesendutils {private properties props; //システムプロパティプライベートセッションセッション。 //メールセッションオブジェクトPrivate Mimemessage Mimemsg; // MIMEメールオブジェクトプライベートマルチパートMP; //マルチパートオブジェクト、電子メールコンテンツ、タイトル、添付ファイル、その他のコンテンツがすべて追加され、Mimemessageオブジェクトが生成されます/** * constructor * @param */public MailFilesEndutils(){props = System.getProperties(); props.put( "mail.smtp.auth"、 "false"); session = session.getDefaultInstance(props、null); session.setDebug(true); mimemsg = new Mimemessage(session); mp = new mimemultipart(); } /** * constructor * @param smtp mail sendutils(string smtp、string username、string password){props = system.getProperties(); props.put( "mail.smtp.auth"、 "true"); props.put( "mail.smtp.host"、smtp); props.put( "username"、username); propss.put( "パスワード"、パスワード); session = session.getDefaultInstance(props、null); session.setDebug(true); mimemsg = new Mimemessage(session); mp = new mimemultipart(); } /***電子メールを送信* /public boolean sendmail(string from、string [] to、string subject、string content、string filename){try {// sender mimemsg.setfrom(new InternetAddress(from)); //(int i = 0; i <to.length; i ++){mimemsg.setRecipients(message.RecipientType.to、InternetAddress.Parse([i])); } // set cc // for(int i = 0; i <copyto.length; i ++){// mimemsg.setrecipients(message.recipienttype.cc、internetadddress.parse(copyto [i])); //} //トピックを設定するmimemsg.setsubject(subject); //テキストBodyPart bp = new MimeBodyPart()を設定します。 bp.setcontent(content、 "text/html; charset = utf-8"); mp.addbodypart(bp); //添付ファイルを設定bp = new MimeBodyPart(); filedatasource files = new filedatasource(filename); bp.setdatahandler(新しいDatahandler(申請)); bp.setfileName(mimeutility.encodetext(fileds.getName()、 "utf-8"、 "b")); mp.addbodypart(bp); mimemsg.setContent(MP); mimemsg.savechanges(); //電子メールを送信するif(props.get( "mail.smtp.auth")。 Transport.Connect((string)props.get( "mail.smtp.host")、(string)props.get( "username")、(string)props.get( "password")); Transport.sendmessage(mimemsg、mimemsg.getRecipients(message.recipientType.to)); // Transport.sendmessage(mimemsg、mimemsg.getRecipients(message.recipienttype.cc)); Transport.Close(); } else {transport.send(mimemsg); } system.out.println( "メール送信に正常に"); } catch(messagingexception e){// todo auto-fenated catch block e.printstacktrace(); } catch(unsupportedencodingexception e){// todo auto-enerated catch block e.printstacktrace(); } trueを返します。 } // public void tosendmail(sendmailparam sendmailparam){// mailfilesendutils email = new mailfilesendutils(sendmailparam.getsmtp()、sendmailparam.getusername()、sendmailparam.getpassword(); sendmailparam.getto()、sendmailparam.getsubject()、sendmailparam.getContent()、sendmailparam.getfilepath()); //} public static void main(string [] args){string smtp = "smtp.exmail.qq.com";文字列username = "メールアカウントの送信";文字列パスワード= "電子メールパスワードの送信"; = "send email"からstring; string [] to = {"電子メールアドレスを受信するメールアドレス"}; // string [] copyto = {"CCCメールアドレス"};文字列件名= "件名6";文字列コンテンツ= "メールコンテンツ6";文字列filename = "添付ファイル"; MailFilesEndutils email = new MailFilesEndutils(smtp、username、password); // email.sendmail(from、copyto、subject、content、filename); email.sendmail(from、to、subject、content、filename); }}(添付ファイル:SSLバージョン)
public class mailfilesendutils {private properties props; //システムプロパティプライベートセッションセッション。 //メールセッションオブジェクトPrivate Mimemessage Mimemsg; // MIMEメールオブジェクトプライベートマルチパートMP; //マルチパートオブジェクト、電子メールコンテンツ、タイトル、添付ファイル、その他のコンテンツがすべて追加され、Mimemessageオブジェクトが生成されます/** * constructor * @param */public MailFilesEndutils(){props = System.getProperties(); props.put( "mail.smtp.auth"、 "false"); session = session.getDefaultInstance(props、null); session.setDebug(true); mimemsg = new Mimemessage(session); mp = new mimemultipart(); } /** * constructor * @param smtp mail sendutils(string smtp、string username、string password){security.addprovider(new com.sun.net.ssl.internal.sl.provider());最終文字列ssl_factory = "javax.net.ssl.sslsocketfactory"; props = system.getProperties(); mailsslsocketFactory sf = null; try {sf = new MailSlsocketFactory(); } catch(GeneralAlsecurityException e){} sf.setTrustallhosts(true); props.put( "mail.smtp.auth"、 "true"); props.put( "mail.smtp.host"、smtp); props.put( "mail.smtp.socketfactory.class"、ssl_factory); props.put( "mail.smtp.socketfactory.fallback"、 "false"); props.put( "mail.smtp.ssl.enable"、 "true"); props.put( "mail.smtp.port"、 "465"); props.put( "mail.smtp.ssl.socketfactory"、sf); // props.put( "username"、username); // props.put( "password"、password); session = session.getInstance(props、new Authenticator(){@Override Protected PasswordAuthentication getPassWordAuthentication(){return new PasswordAuthentication(username、password);}}); session.setDebug(true); mimemsg = new Mimemessage(session); mp = new mimemultipart(); } /***電子メールを送信* /public boolean sendmail(string from、string [] to、string subject、string content、string filename){try {// sender mimemsg.setfrom(new InternetAddress(from)); //(int i = 0; i <to.length; i ++){mimemsg.setRecipients(message.RecipientType.to、InternetAddress.Parse(to [i])); } //(int i = 0; i <copyto.length; i ++){// mimemsg.setRecipients(message.recipienttype.cc、internetadddress.parse(copyto [i])); //} //トピックトピックMimemsg.setsubject(subject); //テキストBodyPart bp = new MimeBodyPart()を設定します。 bp.setcontent(content、 "text/html; charset = utf-8"); mp.addbodypart(bp); //添付ファイルを設定bp = new MimeBodyPart(); filedatasource files = new filedatasource(filename); bp.setdatahandler(新しいDatahandler(申請)); bp.setfileName(mimeutility.encodetext(fileds.getName()、 "utf-8"、 "b")); mp.addbodypart(bp); mimemsg.setContent(MP); mimemsg.savechanges(); // mail if(props.get( "mail.smtp.auth")。equals( "true")){Transport Transport = Session.GetTransport( "SMTP"); Transport.Connect((string)props.get( "mail.smtp.host")、(string)props.get( "username")、(string)props.get( "password")); Transport.sendmessage(mimemsg、mimemsg.getRecipients(message.RecipientType.to)); // Transport.SendMessage(Mimemsg、Mimemsg.getRecipients(message.RecipientType.cc)); Transport.Close(); } else {transport.send(mimemsg); } system.out.println( "Mailsが正常に送信される"); } catch(messagingexception e){e.printstacktrace(); } catch(unsupportedencodingexception e){e.printstacktrace(); } trueを返します。 } public boolean tosendmail(sendmailparam sendmailparam){mailfilesendutils email = new mailfilesendutils(sendmailparam.getsmtp()、sendmailparam.getusername()、sendmailparam.getpassword(); email.sendmail(sendmailparam.getfrom()、sendmailparam.getto()、sendmailparam.getsubject()、sendmailparam.getContent()、sendmailparam.getfilepath()); trueを返します。 } // public static void main(string [] args){// string smtp = "smtp.mxhichina.com"; // string username = "emailbox"; // string password = "email password"; // string from = "; // string [] to = {" send "}; /// "huawei"; // string content = "email content 6666"; // string filename = "gdt-3583118353-ad-20170823.xls"; // mailfilesendutils email = new mailfilesendutils(smtp、username、password); /// email.sendmail(from、to、subject、content、filename); //}}}プロジェクトでは、メイン方法にコメントしてから、toSendmail(sendmailparam sendmailparam)を使用します。
ここで定義されているsendmailparamは次のとおりです。
public class sendmailparam {private string smtp;プライベート文字列ユーザー名;プライベート文字列パスワード。プライベート文字列fromプライベート文字列件名; //件名のプライベート文字列コンテンツ; //電子メールコンテンツプライベート文字列filepath; //パブリックを取得するパスsendmailparam(){this.smtp = "smtp.exmail.qq.com"; // example this.username = "emailアカウント"; this.password = "電子メールパスワード"; this.from = "email"; this.subject = ""; this.content = ""; this.filepath = ""; } public string getSmtp(){return smtp; } public void setSmtp(string smtp){this.smtp = smtp; } public string getUsername(){return username; } public void setUsername(string username){this.username = username; } public string getPassWord(){パスワードを返します。 } public void setPassword(string password){this.password = password; } public string getFrom(){return; } public void setfrom(string from){this.from = from; } public string [] getto(){return to; } public void setto(string [] to){this.to = to; } public string getSubject(){return subject; } public void setSubject(string subject){this.subject = subject; } public string getContent(){return content; } public void setContent(string content){this.content = content; } public string getFilepath(){return filepath; } public void setfilepath(string filepath){this.filepath = filepath; }}Maven依存関係パッケージ
<Dependency> GroupId> Javax.Mail </groupId> <ArtifActid>メール</artifactid> <バージョン> 1.4.7 </version> </dependency>
Gradle依存関係パッケージ
「javax.mail:メール:1.4.7」をコンパイルする
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。