在生活中,很多人都不知道成都java学习中心-地址-电话是什么意思,其实他的意思是非常简单的,下面就是小编搜索到的成都java学习中心-地址-电话相关的一些知识,我们一起来学习下吧!
(资料图)
相关知识点:锘縫ackage com.nerve.cloudoffice.common.util;
import .util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class EMailSender {
private static final long serialVersionUID=1L;
private MimeMessage mimeMsg; // MIME???????
private Session session; // ?????????
private Properties props; // ??????
private boolean needAuth=false; // smtp?????????
private String username=""; // smtp??????????????
private String password="";
private Multipart mp; // Multipart????,???????,????,??????????????????泻???????
private String log;
public EMailSender() {
}
public EMailSender(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
}
public void setSmtpHost(String hostName) {
System.out.println("???????????mail.smtp.host=" + hostName);
if (props==null)
props=System.getProperties(); // ????????????
props.put("mail.smtp.host", hostName); // ????SMTP????
props.put("mail.smtp.localhost", "localHostAdress");
}
public boolean createMimeMessage() {
try {
System.out.println("???????????????");
session=Session.getDefaultInstance(props, null); // ????????????
} catch (Exception e) {
log="?????????????????????" + e.toString();
System.err.println(log);
return false;
}
try {
mimeMsg=new MimeMessage(session); // ????MIME???????
mp=new MimeMultipart(); // mp ???multipart????
// Multipart is a container that holds multiple body parts.
return true;
} catch (Exception e) {
log="????MIME???????????" + e;
System.err.println(log);
return false;
}
}
public void setNeedAuth(boolean need) {
System.out.println("????smtp????????mail.smtp.auth=" + need);
if (props==null)
props=System.getProperties();
if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}
public void setNamePass(String name, String pass) {
System.out.println("?????????????????");
username=name;
password=pass;
}
public boolean setSubject(String mailSubject) {
System.out.println("???????????");
try {
mimeMsg.setSubject(MimeUtility.encodeText(mailSubject,"utf-8","B"));
return true;
} catch (Exception e) {
log="?????????????????"+e;
System.err.println(log);
return false;
}
}
public boolean setBody(String mailBody) {
try {
System.out.println("???????????");
BodyPart bp=new MimeBodyPart();
// ???????????
bp.setContent(
""
+ mailBody, "text/html;charset=utf-8");
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
log="????????????????????" + e;
System.err.println(log);
return false;
}
}
public boolean setFiles(List files){
try{
for(String s:files){
MimeBodyPart mbp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(s); //????????
mbp.setDataHandler(new DataHandler(fds)); //????????????????BodyPart
mbp.setFileName(fds.getName()); //???????????????BodyPart
mp.addBodyPart(mbp);
}
return true;
}catch(Exception e){
log="?????????????"+e;
e.printStackTrace();
return false;
}
}
public boolean addFile(String path, String name){
try{
MimeBodyPart mbp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(path); //????????
mbp.setDataHandler(new DataHandler(fds)); //????????????????BodyPart
mbp.setFileName(MimeUtility.encodeText(name,"utf-8","B"));
mp.addBodyPart(mbp);
return true;
}catch(Exception e){
log="?????????????"+e;
e.printStackTrace();
return false;
}
}
public boolean setFrom(String from) {
System.out.println("???梅??????");
try {
mimeMsg.setFrom(new InternetAddress(from)); // ???梅?????
return true;
} catch (Exception e) {
log="???梅????????:"+e;
return false;
}
}
public boolean setTo(String to) {
System.out.println("??????????");
if (to==null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));
return true;
} catch (Exception e) {
return false;
}
}
public boolean setCopyTo(String copyto) {
if (copyto==null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(copyto));
return true;
} catch (Exception e) {
return false;
}
}
public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("??????????....");
Session mailSession=Session.getInstance(props, null);
Transport transport=mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username,
password);
transport.sendMessage(mimeMsg, mimeMsg
.getRecipients(Message.RecipientType.TO));
// transport.send(mimeMsg);
System.out.println("????????????");
transport.close();
return true;
} catch (Exception e) {
log="???????????" + e;
System.err.println(log);
return false;
}
}
public String getLog(){
return log;
}
}