java使用代理发送邮件_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java使用代理发送邮件

java使用代理发送邮件

 2011/12/28 17:55:40  googgoogle  http://googgoogle.iteye.com  我要评论(0)
  • 摘要:[size=18px;]最近用java写个发邮件的东东,然后打成jar包,jar包可以放在任何地方邮件正常发送[/size][size=18px;]要求:[/size][size=18px;]1、网络正常的情况下,可以发送;[/size][size=18px;]2、网络不正常的时候,需要使用内网连接到外网,使用代理发送邮件。[/size][size=18px;]部分代码拿出来分享一下:[/size][size=16px;]packagecom.zyuc.mail
  • 标签:使用 Java 邮件 代理 发送邮件

    [size=18px;]最近用java写个发邮件的东东,然后打成jar包,jar包可以放在任何地方邮件正常发送[/size]

[size=18px;]要求 :[/size]

[size=18px;]1、网络正常的情况下,可以发送;[/size]

[size=18px;]2、网络不正常的时候,需要使用内网连接到外网,使用代理发送邮件。[/size]

[size=18px;]部分代码拿出来分享一下:[/size]

[size=16px;]package com.zyuc.mail;[/size]

[size=16px;]import java.io.File;<br>
import java.util.HashMap;<br>
import java.util.Map;<br>
import java.util.Properties;<br>
import javax.activation.*;<br>
import javax.mail.*;<br>
import javax.mail.internet.*;<br>
import org.apache.log4j.Logger;<br>
import com.zyuc.domParse.DomParseXml;<br>
import com.zyuc.util.UtilTools;[/size]

[size=16px;]/**<br>
* 邮件发送<br>
* @author wangxw<br>
* 2011-10-26<br>
*/<br>
public class JmailConfig {<br><br>
private static Logger logger = Logger.getLogger(JmailConfig.class);<br>
private static Map map = new HashMap();<br>
private static JmailFileRead jmailFileRead = new JmailFileRead();<br>
public String Email_Text = new String();<br>
private static JmailConfig mail = new JmailConfig();<br><br>
/**<br>
*构造函数 初始化参数信息<br>
*/<br>
public JmailConfig(){<br>
//获得配置文件<br>
String configPath = UtilTools.getPath()+File.separator+"JmailConfig.xml";<br>
File file = new File(configPath);<br>
//加载配置文件信息<br>
if(file.exists()){<br>
map = new DomParseXml().parseXml(configPath);<br>
Email_Text = jmailFileRead.fileRead(map.get("Email_Text").toString()); <br>
}else{<br>
logger.error("邮件配置文件未找到");<br>
}<br>
}<br><br>
/**<br>
* 邮件附件的添加<br>
* @param multipart<br>
* @param filePath<br>
* @param fileName<br>
* @param k<br>
* @return boolean<br>
*/<br>
private boolean addMultipart(Multipart multipart, String filePath,<br>
String fileName, int k) {<br>
boolean flag = true;<br>
try {<br>
MimeBodyPart messageBodyPart = new MimeBodyPart();<br>
if (k == 0) {<br>
messageBodyPart.setText(Email_Text);<br>
multipart.addBodyPart(messageBodyPart);<br>
}<br>
messageBodyPart = new MimeBodyPart();<br>
DataSource source = new FileDataSource(filePath);<br>
messageBodyPart.setDataHandler(new DataHandler(source));<br>
messageBodyPart.setFileName(fileName);<br>
multipart.addBodyPart(messageBodyPart);<br>
} catch (Exception e) {<br>
flag = false;<br>
logger.error("邮件附件的添加失败" + e.getMessage());<br>
e.printStackTrace();<br>
}<br>
return flag;<br>
}<br><br>
/**<br>
* 发送邮件信息配置<br>
* @return boolean<br>
*/<br>
public boolean sendMail(){<br>
boolean sendFlag = true;<br>
final String username = map.get("Email_User").toString();<br>
final String password = map.get("Email_Pswd").toString();<br><br>
Session session;<br>
//获得是否使用代理发送的标识<br>
String Email_Proxy_Flag = map.get("Email_Proxy_Flag").toString();<br>
if("1".equalsIgnoreCase(Email_Proxy_Flag)){<br>
//若Email_Proxy_Flag==1则使用代理发送<br>
try {<br>
Properties props = System.getProperties();<br>
props.put("mail.smtp.auth", "true");<br>
props.setProperty("proxySet", "true");<br>
props.setProperty("mail.stmp.host", map.get("Email_Host").toString());<br>
props.setProperty("http.proxyhost",map.get("Email_Proxy_Host").toString());<br>
props.setProperty("http.proxyport",map.get("Email_Proxy_Port").toString());<br><br>
session = Session.getDefaultInstance(props,<br>
new Authenticator() {<br>
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {<br>
return new javax.mail.PasswordAuthentication(<br>
username, password);<br>
}<br>
});<br>
MimeMessage message = new MimeMessage(session);<br>
message.setFrom(new InternetAddress(map.get("Email_From").toString()));<br><br>
//多个接受者<br>
String[] to = map.get("Email_To").toString().split(";");<br>
if (to != null) {<br>
InternetAddress[] toAddress = new InternetAddress[to.length];<br>
for (int i = 0; i < to.length; i++) {<br>
toAddress[i] = new InternetAddress(to[i]);<br>
}<br>
message.addRecipients(Message.RecipientType.TO, toAddress);<br>
}<br><br>
//多个抄送者<br>
String[] cc = map.get("Email_Cc").toString().split(";");<br>
if (null != cc) {<br>
InternetAddress[] ccAddress = new InternetAddress[cc.length];<br>
for (int j = 0; j < cc.length; j++) {<br>
ccAddress[j] = new InternetAddress(cc[j]);<br>
}<br>
message.addRecipients(Message.RecipientType.CC, ccAddress);<br>
}<br>
message.setSubject(map.get("Email_Subject").toString());<br><br><br>
//附件功能<br>
Multipart multipart = new MimeMultipart();<br>
String[] filepath = map.get("FilePath").toString().split(";");<br>
String[] attachName = map.get("AttachName").toString().split(";");<br>
if (null != filepath) {<br>
for (int k = 0; k < filepath.length; k++) {<br>
addMultipart(multipart, filepath[k], attachName[k], k);<br>
}<br>
} else if (null == filepath) {<br>
message.setText(Email_Text);<br>
}<br>
message.setContent(multipart);<br><br>
Transport transport = session.getTransport("smtp");<br>
transport.connect(map.get("Email_Host").toString(), username, password);<br>
transport.sendMessage(message, message.getAllRecipients());<br>
transport.close();<br>
} catch (Exception e) {<br>
sendFlag = false;<br>
logger.error("使用代理发送邮件出错" + e.getMessage());<br>
e.printStackTrace();<br>
}<br>
}else{<br>
//若Email_Proxy_Flag!=1则不使用代理发送<br>
try {<br>
Properties props = System.getProperties();<br>
props.put("mail.smtp.auth", "true");<br>
props.setProperty("mail.stmp.host", map.get("Email_Host").toString());<br><br>
session = Session.getDefaultInstance(props,<br>
new Authenticator() {<br>
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {<br>
return new javax.mail.PasswordAuthentication(<br>
username, password);<br>
}<br>
});<br>
MimeMessage message = new MimeMessage(session);<br>
message.setFrom(new InternetAddress(map.get("Email_From").toString()));<br><br>
//多个接收者<br>
String[] to = map.get("Email_To").toString().split(";");<br>
if (to != null) {<br>
InternetAddress[] toAddress = new InternetAddress[to.length];<br>
for (int i = 0; i < to.length; i++) {<br>
toAddress[i] = new InternetAddress(to[i]);<br>
}<br>
message.addRecipients(Message.RecipientType.TO, toAddress);<br>
}<br><br>
//多个抄送者<br>
String[] cc = map.get("Email_Cc").toString().split(";");<br>
if (null != cc &amp;&amp; cc.length > 0) {<br>
InternetAddress[] ccAddress = new InternetAddress[cc.length];<br>
for (int j = 0; j < cc.length; j++) {<br>
ccAddress[j] = new InternetAddress(cc[j]);<br>
}<br>
message.addRecipients(Message.RecipientType.CC, ccAddress);<br>
}<br>
message.setSubject(map.get("Email_Subject").toString());<br><br>
//附件功能<br>
Multipart multipart = new MimeMultipart();<br>
String[] filepath = map.get("FilePath").toString().split(";");<br>
String[] attachName = map.get("AttachName").toString().split(";");<br>
if (null != filepath &amp;&amp; filepath.length > 0 &amp;&amp; attachName.length > 0 &amp;&amp; filepath.length == attachName.length) {<br>
for (int k = 0; k < filepath.length; k++) {<br>
addMultipart(multipart, filepath[k], attachName[k], k);<br>
}<br>
} else if (null == filepath) {<br>
message.setText(Email_Text);<br>
}<br>
message.setContent(multipart);<br><br>
Transport transport = session.getTransport("smtp");<br>
transport.connect(map.get("Email_Host").toString(), username, password);<br>
transport.sendMessage(message, message.getAllRecipients());<br>
transport.close();<br>
} catch (Exception e) {<br>
sendFlag = false;<br>
logger.error("发送邮件出错" + e.getMessage());<br>
e.printStackTrace();<br>
}<br>
}<br>
return sendFlag;<br>
}<br><br>
public String returnMessage(){<br>
boolean flag = mail.sendMail();<br>
StringBuffer sb = new StringBuffer();<br>
if(flag){<br>
sb.append("\n").append("邮件发送已成功").append("\t").append("\n");<br>
sb.append("邮件发送者:" + map.get("Email_From").toString()).append("\t").append("\n");<br>
sb.append("邮件发送至:" + map.get("Email_To").toString()).append("\t").append("\n");<br>
sb.append("邮件抄送给:" + map.get("Email_Cc").toString()).append("\t").append("\n");<br>
sb.append("邮件正文为:" + map.get("Email_Text").toString()).append("\t").append("\n");<br>
sb.append("发送时间是:" + UtilTools.getDate()).append("\t").append("\n");<br>
}else{<br>
sb.append("\n").append("邮件发送失败").append("\t").append("\n");<br>
sb.append("发送时间是:" + UtilTools.getDate()).append("\t").append("\n");<br>
}<br>
return sb.toString();<br>
}<br><br>
public static void main(String args[]) {<br>
logger.info("===============开始发送=================");<br>
logger.info(mail.returnMessage());<br>
logger.info("===============发送结束=================");<br>
}<br>
}<br>[/size]



 
发表评论
用户名: 匿名