package gui;import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
import javax.activation.FileDataSource;
import javax.activation.DataHandler; /**
* @author Bromon
*/
public class SenderWithSMTPRar
{
String host="";
String user="";
String password=""; public void setHost(String host)
{
this.host=host;
} public void setAccount(String user,String password)
{
this.user=user;
this.password=password;
} public void send(String from,String to,String subject,String content)
{
Properties props = new Properties();
props.put("mail.smtp.host", host);//指定SMTP服务器
props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
try
{
Session mailSession = Session.getDefaultInstance(props);
MimeMessage msg = new MimeMessage(mailSession);
//msg.setSubject("hello");
msg.setFrom(new InternetAddress(from)); //发件人
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); //收件人
msg.setSubject(subject); //邮件主题
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setContent(content,"text/html"); MimeBodyPart fileBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource("GIS.rar"); //要发送的附件
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName(fds.getName());
Multipart container = new MimeMultipart();
container.addBodyPart(textBodyPart);
container.addBodyPart(fileBodyPart);
msg.setContent(container);
msg.setSentDate(new Date());
Transport.send(msg); }catch(Exception e)
{
System.out.println(e);
} } public static void main(String args[])
{
SenderWithSMTPRar sm=new SenderWithSMTPRar();
String content="test"; sm.setHost("smtp.yeah.net");//指定要使用的邮件服务器
sm.setAccount("luohui","******");//指定帐号和密码 /*
* @param String 发件人的地址
* @param String 收件人地址
* @param String 邮件标题
* @param String 邮件正文
*/
sm.send("[email protected]","[email protected]","test",content);
} }
import java.util.*;
import javax.mail.internet.*;
import javax.activation.FileDataSource;
import javax.activation.DataHandler; /**
* @author Bromon
*/
public class SenderWithSMTPRar
{
String host="";
String user="";
String password=""; public void setHost(String host)
{
this.host=host;
} public void setAccount(String user,String password)
{
this.user=user;
this.password=password;
} public void send(String from,String to,String subject,String content)
{
Properties props = new Properties();
props.put("mail.smtp.host", host);//指定SMTP服务器
props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
try
{
Session mailSession = Session.getDefaultInstance(props);
MimeMessage msg = new MimeMessage(mailSession);
//msg.setSubject("hello");
msg.setFrom(new InternetAddress(from)); //发件人
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); //收件人
msg.setSubject(subject); //邮件主题
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setContent(content,"text/html"); MimeBodyPart fileBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource("GIS.rar"); //要发送的附件
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName(fds.getName());
Multipart container = new MimeMultipart();
container.addBodyPart(textBodyPart);
container.addBodyPart(fileBodyPart);
msg.setContent(container);
msg.setSentDate(new Date());
Transport.send(msg); }catch(Exception e)
{
System.out.println(e);
} } public static void main(String args[])
{
SenderWithSMTPRar sm=new SenderWithSMTPRar();
String content="test"; sm.setHost("smtp.yeah.net");//指定要使用的邮件服务器
sm.setAccount("luohui","******");//指定帐号和密码 /*
* @param String 发件人的地址
* @param String 收件人地址
* @param String 邮件标题
* @param String 邮件正文
*/
sm.send("[email protected]","[email protected]","test",content);
} }
public boolean sendAlertMail(AlertConfBean alertConfBean)
{
String xmailer = null; String subject = null; String contentType = null; Properties properties = new Properties(); // SMTP認証フラグ
String enableSMTPAuth = alertConfBean.getAuthPolicy(); content = createAlertMail(alertConfBean);
userID = alertConfBean.getAuthUserID();
password = alertConfBean.getAuthPassword();
if (content.equals("notSend"))
{
return true;
}
smtpHost = alertConfBean.getMailServer();
String port = alertConfBean.getMailPort(); Session session = null; properties.put("mail.smtp.host", smtpHost);
//properties.put("mail.debug", "true");
// SMTP認証
if (enableSMTPAuth.equals(SMTP_USE))
{
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", port);
}
// 非SMTP認証
else{
properties.put("mail.smtp.auth", "false");
}
session = Session.getInstance(properties, null); // ユーザIDとパスワードを設定する // Mimeメールオブジェクトを作成する
MimeMessage mimeMsg = new MimeMessage(session);
try
{ Iterator iterKey = headTemplateData.keySet().iterator();
Iterator iterValue = headTemplateData.values().iterator();
while (iterKey.hasNext())
{
String key = iterKey.next().toString();
String value = iterValue.next().toString();
if (!value.equals(""))
{
mimeMsg.setHeader(key, value);
}
if (key.equals("From") || key.equals("Subject")
|| key.equals("X-mailer") || key.equals("Content-Type"))
{
continue;
}
} if (headTemplateData.get("X-mailer") == null
|| headTemplateData.get("X-mailer").toString().equals(""))
{
xmailer = "Systemwalker Desktop Inspection";
} else
{
xmailer = headTemplateData.get("X-mailer").toString();
}
mimeMsg.setHeader("X-mailer", xmailer); if (headTemplateData.get("Subject") == null
|| headTemplateData.get("Subject").toString().equals(""))
{
subject = "Unauthorized access occurred.";
} else
{
subject = headTemplateData.get("Subject").toString();
}
if (headTemplateData.get("From") == null
|| headTemplateData.get("From").toString().equals(""))
{
sender = alertConfBean.getMailAddress();
} else
{
sender = headTemplateData.get("From").toString();
}
if (headTemplateData.get("Content-Type") == null
|| headTemplateData.get("Content-Type").toString().equals(
""))
{
contentType = "iso-2022-jp";
} else
{
contentType = headTemplateData.get("Content-Type").toString();
} // 送信アドレスをMimeメールオブジェクトに設定する
mimeMsg.setFrom(new InternetAddress(sender));
mimeMsg.setSubject(subject, contentType);
mimeMsg.setText(content, contentType);
// 受信アドレスをMimeメールオブジェクトに設定する
mimeMsg.setRecipients(Message.RecipientType.TO, parse(receiver));
mimeMsg.setSentDate(new Date());
if (enableSMTPAuth.equals(SMTP_USE))
{
Transport smtpTransport = session.getTransport("smtp");
smtpTransport.connect(smtpHost, Integer.parseInt(port), userID,
password);
smtpTransport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
smtpTransport.close();
return true;
} Transport.send(mimeMsg);// 送信する
return true;
} catch (Exception exception)
{
m_log.error("MessageException", exception);
return false;
} }
smtpTransport.connect(smtpHost, Integer.parseInt(port), userID,password);
smtpTransport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
smtpTransport.close();
你用这个发发看
Transport smtpTransport = session.getTransport("smtp");
smtpTransport.connect(smtpHost,userID,password);
smtpTransport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
smtpTransport.close();
props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
已经指定了smtp验证,,,
sm.setHost("smtp.yeah.net");//指定要使用的邮件服务器
sm.setAccount("luohui","******");//指定帐号和密码
用户名和密码,而且我用来发送普通的邮件没问题,代附件的就报错了,,晕
-----------------发送普通邮件代码:
package gui; import javax.mail.*;
import java.util.*;
import javax.mail.internet.*; /**
* @author Bromon
*/
public class SenderWithSMTPVer
{
String host="";
String user="";
String password=""; public void setHost(String host)
{
this.host=host;
} public void setAccount(String user,String password)
{
this.user=user;
this.password=password;
} public void send(String from,String to,String subject,String content)
{
Properties props = new Properties();
props.put("mail.smtp.host", host);//指定SMTP服务器
props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
try
{
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);//是否在控制台显示debug信息
Message message=new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));//发件人
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));//收件人
message.setSubject(subject);//邮件主题
//message.setText(content);//邮件内容
message.setContent(content,"text/html;charset=gb2312");
message.saveChanges(); Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}catch(Exception e)
{
System.out.println(e);
} } public static void main(String args[])
{
SenderWithSMTPVer sm=new SenderWithSMTPVer();
String content="<html><body><h1>你们好</body></html>"; sm.setHost("smtp.yeah.net");//指定要使用的邮件服务器
sm.setAccount("luohui","******");//指定帐号和密码 /*
* @param String 发件人的地址
* @param String 收件人地址
* @param String 邮件标题
* @param String 邮件正文
*/
sm.send("[email protected]","[email protected]","test",content);
} }
Session mailSession = Session.getInstance(props,null);看看
if (args.length != 5) {
System.out.println("usage: java sendfile <to> <from> <smtp> <file> true|false");
System.exit(1);
} String to = args[0];
String from = args[1];
String host = args[2];
String filename = args[3];
boolean debug = Boolean.valueOf(args[4]).booleanValue();
String msgText1 = "Sending a file.\n";
String subject = "Sending a file";
// create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session = Session.getInstance(props, null);
session.setDebug(debug);
try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject); // create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1); // create the second message part
MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName()); // create the Multipart and add its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2); // add the Multipart to the message
msg.setContent(mp); // set the Date: header
msg.setSentDate(new Date());
// send the message
Transport.send(msg);
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
}
}
transport.connect(host, user, password);了,,谢谢