之前一直在做本地郵件伺服器傳送,可是沒有成功,都說什麼反解析失敗什麼的原因,具體的也不太清楚,
最近又學習了用java做郵件傳送伺服器,還真的成功了分享給大家(以下發送只包括文字內容,不包括附加及圖片,如有不明白的可以聯絡我)
java執行:main執行測試的話,包需要java2E 1.4
javaweb執行:包需要javaEE 5
執行呼叫SendMailGUI中方法run("傳送方地址(隨便起名,但要符合郵件樣式@.)", "接收方地址", "傳送主題", "傳送內容")
類:SendMailGUI
package action;
import java.util.Arrays;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.Attributes;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.Message;
import javax.mail.internet.MimeMultipart;
import javax.mail.BodyPart;
import javax.mail.internet.MimeBodyPart;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import javax.mail.internet.MimeUtility;
import javax.mail.Multipart;
import java.util.Date;
import javax.mail.Transport;
public class SendMailGUI{
/** serialVersionUID */
String imagePath = null;
javax.swing.JProgressBar bar = new javax.swing.JProgressBar();
public int run(String from, String to, String title, String content) {
MailMessage mesg = new MailMessage(from,to,title,content);
try
{
sendMail(mesg);
return 1;
}catch (Exception ex){
return 0;
}
private void sendMail(MailMessage mesg) throws Exception
//DNS伺服器
String dns = "dns://";
//得到接受方的伺服器域名
String domain = mesg.getTo().substring(mesg.getTo().indexOf("@") + 1);
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
env.put(Context.PROVIDER_URL, dns);
DirContext ctx = new InitialDirContext(env);
Attributes attrs = ctx.getAttributes(domain, new String[] {"MX"});
if (attrs == null || attrs.size() <= 0) {
throw new java.lang.IllegalStateException("錯誤:你的DNS伺服器沒有相應的MX記錄!");
NamingEnumeration servers = attrs.getAll();
// 列出所有郵件伺服器:
String smtpHost = null;
boolean isSend = false;
St
之前一直在做本地郵件伺服器傳送,可是沒有成功,都說什麼反解析失敗什麼的原因,具體的也不太清楚,
最近又學習了用java做郵件傳送伺服器,還真的成功了分享給大家(以下發送只包括文字內容,不包括附加及圖片,如有不明白的可以聯絡我)
java執行:main執行測試的話,包需要java2E 1.4
javaweb執行:包需要javaEE 5
執行呼叫SendMailGUI中方法run("傳送方地址(隨便起名,但要符合郵件樣式@.)", "接收方地址", "傳送主題", "傳送內容")
類:SendMailGUI
package action;
import java.util.Arrays;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.Attributes;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.Message;
import javax.mail.internet.MimeMultipart;
import javax.mail.BodyPart;
import javax.mail.internet.MimeBodyPart;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import javax.mail.internet.MimeUtility;
import javax.mail.Multipart;
import java.util.Date;
import javax.mail.Transport;
public class SendMailGUI{
/** serialVersionUID */
String imagePath = null;
javax.swing.JProgressBar bar = new javax.swing.JProgressBar();
public int run(String from, String to, String title, String content) {
MailMessage mesg = new MailMessage(from,to,title,content);
try
{
sendMail(mesg);
return 1;
}catch (Exception ex){
return 0;
}
}
private void sendMail(MailMessage mesg) throws Exception
{
//DNS伺服器
String dns = "dns://";
//得到接受方的伺服器域名
String domain = mesg.getTo().substring(mesg.getTo().indexOf("@") + 1);
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
env.put(Context.PROVIDER_URL, dns);
DirContext ctx = new InitialDirContext(env);
Attributes attrs = ctx.getAttributes(domain, new String[] {"MX"});
if (attrs == null || attrs.size() <= 0) {
throw new java.lang.IllegalStateException("錯誤:你的DNS伺服器沒有相應的MX記錄!");
}
NamingEnumeration servers = attrs.getAll();
// 列出所有郵件伺服器:
String smtpHost = null;
boolean isSend = false;
St