Javaはjavaxを使用する.mail.JArメールの送信と添付ファイルの送信を許可
15563 ワード
Javaはウェブページの开発において绝大な优位を占めているため、ウェブ端のリーダーとして、例えばメールの送信やメールの送信などが必然となり、サイバーセキュリティの再セキュリティはアカウントのセキュリティレベルをもっと高くする必要があります!そのため、これらは開発者にとって身につけなければならないスキルになります!私はずっと开発者として、开発の过程の中でどれだけの难题に出会うことを恐れないで、それを解决する勇気と决心があるかどうか、ここで多く学ぶことが肝心になって、1つのプログラム猿の発展の重要な中の重さになりました!よし、おしゃべりはここまでにして、実際のことをしなさい.
ある開発者がある程度働くと、対象に向かう思考はずっと彼の老子の中で囲まれています!だから私たちはまずメールの定数クラス、実体クラス、ツールクラスなどをカプセル化して、継承するべき継承をします!コードが来た、目を大きく開けて:
メールを送るときに欠かせないメールのタイトルやメールの内容は、メールとして広く使われているものとしてカプセル化されています.では、メールボックスのログインクラスを見てみましょう.
必要な定数クラスが入ってくるはずです
肝心なところが来たので、メールを送る肝心な部分を見てみましょう.
Webサイトでのアクティベーションコードや認証コードなどは言うまでもないでしょう
Okはjavaxを利用する.mail.jarがメールを送ると大成功!とてもeasyではありませんか!完全なdemoリソース;ダウンロード先:http://download.csdn.net/download/luo201227/7446279
しばらくの間、更新してみましたが、実は何も言うことはありません.ただ、興味があれば、Jamesメールサーバーの構築を理解してみてください.
ある開発者がある程度働くと、対象に向かう思考はずっと彼の老子の中で囲まれています!だから私たちはまずメールの定数クラス、実体クラス、ツールクラスなどをカプセル化して、継承するべき継承をします!コードが来た、目を大きく開けて:
/**
* @Description:
*
* @Title: SimpleMail.java
* @Package com.joyce.bean
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 09:06:51
* @version V2.0
*/
package com.joyce.mail.bean;
/**
* @Description:
*
* @ClassName: SimpleMail
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 09:06:51
* @version V2.0
*/
public class Mail {
/**
*
*/
private String subject;
/**
*
*/
private String content;
/**
* @return the subject
*/
public String getSubject() {
return subject;
}
/**
* @param subject
* the subject to set
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content
* the content to set
*/
public void setContent(String content) {
this.content = content;
}
}
メールを送るときに欠かせないメールのタイトルやメールの内容は、メールとして広く使われているものとしてカプセル化されています.では、メールボックスのログインクラスを見てみましょう.
/**
* @Description:
*
* @Title: MailAuthenticator.java
* @Package com.joyce.bean
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 08:59:11
* @version V2.0
*/
package com.joyce.mail.bean;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* @Description:
*
* @ClassName: MailAuthenticator
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 08:59:11
* @version V2.0
*/
public class MailAuthenticator extends Authenticator {
/**
* ( )
*/
private String username;
/**
*
*/
private String password;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @param username
* @param password
*/
public MailAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
必要な定数クラスが入ってくるはずです
package com.joyce.mail.bean;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Description:
*
* @ClassName: MailConstant
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 11:01:16
* @version V2.0
*/
public class MailConstant {
public static final String MAIL_USER = "[email protected]";
public static final String MAIL_PWD = "*****";
public static final boolean MAIL_IFDEBUG = true;
public static final String MAIL_CONTENT_CHARSET = "text/html;charset=utf-8";
public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd HH mm ss E");
public static final String MAIL_TITLE = "********* " + sdf.format(new Date());//
public static String getMailContent(String content){
StringBuffer sb = new StringBuffer();
sb.append("<div style='width:1024px;height:auto;margin:0px auto;background-color:#66CCFF;font-size:14px;font-family: ;border-radius:5px;padding:5px;'><center><h1>");
sb.append("</h1></center><div style='margin-left:20px;margin-bottom:10px;'><b> , !</b><br/><br/>");
sb.append(" <b></b>"+content);
sb.append("</div></div>");
return sb.toString();
}
}
肝心なところが来たので、メールを送る肝心な部分を見てみましょう.
/**
* @Description:
*
* @Title: MailSender.java
* @Package com.joyce.service.impl
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 09:03:08
* @version V2.0
*/
package com.hupu.nac.mail.sender;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.hupu.nac.mail.bean.Mail;
import com.hupu.nac.mail.bean.MailAuthenticator;
import com.hupu.nac.mail.bean.MailConstant;
/**
* @Description:
*
* @ClassName: MailSender
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 09:03:08
* @version V2.0
*/
public class MailSender {
/**
* props
*/
private final transient Properties props = new Properties();
/**
*
*/
private transient MailAuthenticator authenticator;
/**
* session
*/
private transient Session session;
/**
*
*
* @param smtpHostName
* SMTP
* @param username
* ( )
* @param password
*
*/
public MailSender(final String smtpHostName, final String username,
final String password) {
init(username, password, smtpHostName);
}
/**
*
*
* @param username
* ( ), SMTP
* @param password
*
*/
public MailSender(final String username, final String password) {
// smtp ,
final String smtpHostName = "smtp." + username.split("@")[1];
init(username, password, smtpHostName);
}
/**
* @Description:
*
* @param username
* ( )
* @param password
*
* @param smtpHostName
* SMTP
*
* @Title: MailSender.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 09:18:31
* @version V2.0
*/
private void init(String username, String password, String smtpHostName) {
// props
props.put("mail.smtp.host", smtpHostName);
props.put("mail.smtp.auth", "true");
//
authenticator = new MailAuthenticator(username, password);
// session
session = Session.getInstance(props, authenticator);
//
session.setDebug(MailConstant.MAIL_IFDEBUG);
}
/**
* @Description:
*
* @param recipient
*
* @param subject
*
* @param content
*
* @throws AddressException
* @throws MessagingException
*
* @Title: MailSender.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 09:19:16
* @version V2.0
*/
public void send(String recipient, String subject, Object content) throws Exception {
send(recipient, subject, content, null);
}
/**
*
*
* @param recipient
*
* @param subject
*
* @param content
*
* @param files
*
* @throws Exception
* @author Joyce.Luo
* @date 2014-10-15 10:23:09
* @version V3.0
* @since Tomcat6.0,Jdk1.6
* @copyright: Copyright (c) 2014
*/
public void send(String recipient, String subject, Object content, Vector<File> files) throws Exception {
// mime
final MimeMessage message = new MimeMessage(session);
//
message.setFrom(new InternetAddress(authenticator.getUsername()));
//
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
recipient));
//
message.setSubject(subject);
//
if (null == files || files.size() == 0) {
message.setContent(content.toString(), MailConstant.MAIL_CONTENT_CHARSET);
} else {
// Mimemultipart ( )
MimeMultipart multipart = new MimeMultipart();
//MimeBodyPart( / )
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(content.toString(), MailConstant.MAIL_CONTENT_CHARSET);
// MimeMultipart
multipart.addBodyPart(bodyPart);
for (int i = 0; i < files.size(); i++) {
File file = (File) files.elementAt(i);
String fname = file.getName();
// FileDAtaSource( )
FileDataSource fds = new FileDataSource(file);
BodyPart fileBodyPart = new MimeBodyPart();
//
fileBodyPart.setDataHandler(new DataHandler(fds));
//
fileBodyPart.setFileName(fname);
multipart.addBodyPart(fileBodyPart);
message.setContent(multipart);
}
}
//
message.setSentDate(new Date());
//
message.saveChanges();
// message.setFileName(filename)
//
Transport.send(message);
}
/**
* @Description:
*
* @param recipients
*
* @param subject
*
* @param content
*
* @throws AddressException
* @throws MessagingException
*
* @Title: MailSender.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 09:20:24
* @version V2.0
*/
public void send(List<String> recipients, String subject, Object content) throws Exception {
send(recipients, subject, content, null);
}
/**
*
*
* @param recipients
*
* @param subject
*
* @param content
*
* @param files
*
* @throws Exception
* @author Joyce.Luo
* @date 2014-10-15 10:26:35
* @version V3.0
* @since Tomcat6.0,Jdk1.6
* @copyright: Copyright (c) 2014
*/
public void send(List<String> recipients, String subject, Object content, Vector<File> files) throws Exception {
// mime
final MimeMessage message = new MimeMessage(session);
//
message.setFrom(new InternetAddress(authenticator.getUsername()));
//
final int num = recipients.size();
InternetAddress[] addresses = new InternetAddress[num];
for (int i = 0; i < num; i++) {
addresses[i] = new InternetAddress(recipients.get(i));
}
message.setRecipients(Message.RecipientType.TO, addresses);
//
message.setSubject(subject);
//
if (null == files || files.size() == 0) {
message.setContent(content.toString(), MailConstant.MAIL_CONTENT_CHARSET);
} else {
// Mimemultipart ( )
MimeMultipart multipart = new MimeMultipart();
//MimeBodyPart( / )
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(content.toString(), MailConstant.MAIL_CONTENT_CHARSET);
// MimeMultipart
multipart.addBodyPart(bodyPart);
for (int i = 0; i < files.size(); i++) {
File file = (File) files.elementAt(i);
String fname = file.getName();
// FileDAtaSource( )
FileDataSource fds = new FileDataSource(file);
BodyPart fileBodyPart = new MimeBodyPart();
//
fileBodyPart.setDataHandler(new DataHandler(fds));
//
fname = new String(fname.getBytes("UTF-8"), "ISO-8859-1");
fileBodyPart.setFileName(fname);
multipart.addBodyPart(fileBodyPart);
message.setContent(multipart);
}
}
//
message.setSentDate(new Date());
//
message.saveChanges();
//
Transport.send(message);
}
/**
* @Description:
*
* @param recipient
*
* @param mail
*
* @throws Exception
*
* @Title: MailSender.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 09:20:54
* @version V2.0
*/
public void send(String recipient, Mail mail) throws Exception {
send(recipient, mail.getSubject(), mail.getContent());
}
/**
* @Description:
*
* @param recipients
*
* @param mail
*
* @throws Exception
*
* @Title: MailSender.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 09:21:19
* @version V2.0
*/
public void send(List<String> recipients, Mail mail) throws Exception {
send(recipients, mail.getSubject(), mail.getContent());
}
/**
*
*
* @param recipients
*
* @param mail
*
* @param files
*
* @throws Exception
* @author Joyce.Luo
* @date 2014-10-15 10:28:38
* @version V3.0
* @since Tomcat6.0,Jdk1.6
* @copyright: Copyright (c) 2014
*/
public void send(List<String> recipients, Mail mail, Vector<File> files) throws Exception {
send(recipients, mail.getSubject(), mail.getContent(), files);
}
}
Webサイトでのアクティベーションコードや認証コードなどは言うまでもないでしょう
/**
* @Description:
*
* @Title: RandomCodeUtil.java
* @Package com.joyce.mail.util
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 11:07:34
* @version V2.0
*/
package com.joyce.mail.util;
/**
* @Description:
*
* @ClassName: RandomCodeUtil
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 11:07:34
* @version V2.0
*/
public class RandomCodeUtil {
/**
*
*/
private static final String[] randCode = { "0", "1", "2", "3", "4", "5", "6",
"7", "8", "9", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p",
"a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v",
"b", "n", "m" };
/**
* @Description:
*
* @param codeLength
* @return
*
* @Title: RandomCodeUtil.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 11:11:55
* @version V2.0
*/
public static String randomCode(Integer codeLength) throws Exception {
try {
StringBuffer code = new StringBuffer();
if(null == codeLength || 0 == codeLength){
codeLength = 4;
}
for (int i = 0; i < codeLength; i++) {
code.append(randCode[(int)Math.floor(Math.random() * 36)]);
}
return code.toString();
} catch (Exception e) {
throw new RuntimeException("Random Code Error");
}
}
/**
* @Description: 4
*
* @return
* @throws Exception
*
* @Title: RandomCodeUtil.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-5-28 01:19:33
* @version V2.0
*/
public static String randomCode() throws Exception{
return randomCode(null);
}
}
Okはjavaxを利用する.mail.jarがメールを送ると大成功!とてもeasyではありませんか!完全なdemoリソース;ダウンロード先:http://download.csdn.net/download/luo201227/7446279
しばらくの間、更新してみましたが、実は何も言うことはありません.ただ、興味があれば、Jamesメールサーバーの構築を理解してみてください.