자바 웹 stmp 첨부 파일 메 일 보 내기(SSL 버 전 첨부)
public class MailFileSendUtils {
private Properties props; //
private Session session; //
private MimeMessage mimeMsg; //MIME
private Multipart mp; //Multipart , , , 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
*/
public MailFileSendUtils(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);
props.put("password", password);
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 {
//
mimeMsg.setFrom(new InternetAddress(from));
//
for (int i = 0; i < to.length; i++) {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to[i]));
}
//
// for (int i = 0; i < copyto.length; i++) {
// mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.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 fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(MimeUtility.encodeText(fileds.getName(),"UTF-8","B"));
mp.addBodyPart(bp);
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
//
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(" ");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
// public void 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());
// }
public static void main(String[] args) {
String smtp = "smtp.exmail.qq.com";
String username = " ";
String password = " ";
String from = " ";
String[] to = {" "};
// String[] copyto = {" "};
String subject = " 6";
String content = " 6";
String filename = " ";
MailFileSendUtils email = new MailFileSendUtils(smtp, username, password);
// email.sendMail(from, to, copyto, subject, content, filename);
email.sendMail(from, to, subject, content, filename);
}
}
(첨부:SSL 버 전)
public class MailFileSendUtils {
private Properties props; //
private Session session; //
private MimeMessage mimeMsg; //MIME
private Multipart mp; //Multipart , , , 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
*/
public MailFileSendUtils(String smtp,
String username,
String password){
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
props = System.getProperties();
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
} catch (GeneralSecurityException 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 {
//
mimeMsg.setFrom(new InternetAddress(from));
//
for (int i = 0; i < to.length; i++) {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to[i]));
}
//
// for (int i = 0; i < copyto.length; i++) {
// mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.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 fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(MimeUtility.encodeText(fileds.getName(),"UTF-8","B"));
mp.addBodyPart(bp);
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
//
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(" ");
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return 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());
return true;
}
// public static void main(String[] args) {
// String smtp = "smtp.mxhichina.com";
// String username = " ";
// String password = " ";
// String from = " ";
// String[] to = {" "};
//// String[] copyto = {" "};
// String subject = "huawei";
// String content = " 6666";
// String filename = "gdt-3583118353-ad-20170823.xls";
// MailFileSendUtils email = new MailFileSendUtils(smtp, username, password);
//// email.sendMail(from, to, copyto, subject, content, filename);
// email.sendMail(from, to, subject, content, filename);
// }
}
프로젝트 에서 이 도 구 를 사용 합 니 다.main 방법 은 주석 을 달 고 toSendMail(SendMailParam sendMailParam)을 사용 합 니 다.여기 서 정의 하 는 SendMailParam 은:
public class SendMailParam {
private String smtp;
private String username;
private String password;
private String from;//
private String[] to;//
// String[] copyto = {"[email protected]"};
private String subject;//
private String content;//
private String filepath;//
public SendMailParam(){
this.smtp = "smtp.exmail.qq.com";//
this.username = " ";
this.password = " ";
this.from = " ";
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() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFrom() {
return from;
}
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>mail</artifactId>
<version>1.4.7</version>
</dependency>
gradle 의존 패키지
compile "javax.mail:mail:1.4.7"
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.