我写的,发送文字,html,附件,图片 等等...自己看吧try{
Properties props = new Properties();
props.put("mail.smtp.host","ip或域名"); //服务器
props.put("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(props);
SMTPTransport tr = new SMTPTransport(session, null);
tr.connect("61.152.125.84","name", "password"); //用户密码 !!!
MimeMessage message = new MimeMessage(session);
// message.setContent("Hello","text/plain");
message.setSubject(mail_subject);
String htmltext="<h1>hello</h1>"+"<a href=\"http://www.sina.com.cn\" target=\"_blank\">click me please!</a>";
//message.setContent(htmltext,"text/html");//
message.setHeader(mail_head_name,mail_head_value);//原理:可以将邮件看成有多个部分组成的,如正文是一个部分,附件也是一个部分,所以用BodyPart来设置邮件的格式
message.addRecipient(Message.RecipientType.TO,new InternetAddress(mail_to));
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(htmltext,"text/plain") ;
messageBodyPart.setText("testtext");
Multipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart();
String htmlimg = "<h1>hello</h1>"+"<img src=\"cid:memememe\">";//
messageBodyPart.setContent(htmlimg,"text/html") ;
multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource("./test.jpg"); //图片
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","memememe");
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("./jiveDbLog.log");//此处设置邮件的附件
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("test.txt"); multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
message.setSentDate(new java.util.Date());
message.setFrom(new InternetAddress("[email protected]"));
Address toAddress = new InternetAddress(mail_to,"[email protected]");
message.addRecipient(Message.RecipientType.TO,toAddress);//tr.send(message);
Address add[] = new Address[1];
add[0] = toAddress;
tr.sendMessage(message,add) ;
System.out.println("send ok!");
}
catch(Exception ex)
{
ex.printStackTrace() ;
}
Properties props = new Properties();
props.put("mail.smtp.host","ip或域名"); //服务器
props.put("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(props);
SMTPTransport tr = new SMTPTransport(session, null);
tr.connect("61.152.125.84","name", "password"); //用户密码 !!!
MimeMessage message = new MimeMessage(session);
// message.setContent("Hello","text/plain");
message.setSubject(mail_subject);
String htmltext="<h1>hello</h1>"+"<a href=\"http://www.sina.com.cn\" target=\"_blank\">click me please!</a>";
//message.setContent(htmltext,"text/html");//
message.setHeader(mail_head_name,mail_head_value);//原理:可以将邮件看成有多个部分组成的,如正文是一个部分,附件也是一个部分,所以用BodyPart来设置邮件的格式
message.addRecipient(Message.RecipientType.TO,new InternetAddress(mail_to));
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(htmltext,"text/plain") ;
messageBodyPart.setText("testtext");
Multipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart();
String htmlimg = "<h1>hello</h1>"+"<img src=\"cid:memememe\">";//
messageBodyPart.setContent(htmlimg,"text/html") ;
multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource("./test.jpg"); //图片
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","memememe");
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("./jiveDbLog.log");//此处设置邮件的附件
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("test.txt"); multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
message.setSentDate(new java.util.Date());
message.setFrom(new InternetAddress("[email protected]"));
Address toAddress = new InternetAddress(mail_to,"[email protected]");
message.addRecipient(Message.RecipientType.TO,toAddress);//tr.send(message);
Address add[] = new Address[1];
add[0] = toAddress;
tr.sendMessage(message,add) ;
System.out.println("send ok!");
}
catch(Exception ex)
{
ex.printStackTrace() ;
}
解决方案 »
- 现在开发javaweb使用哪个版本的eclipse和myeclipse
- 以下代码声明了几个对象?
- IE后退功能屏蔽问题
- 请教个关于JAVA包的问题
- Spring AOP拦截指定方法
- 动态验证Validate失败
- 基于ext开发的一套自动采集软件,欢迎大家提意见。
- 同样的Web Services,客户端分别用AXIS2和CXF2.5一个报错一个能成功.求解释
- 特急,产品马上就要提交,出现socket程序错误,请大家帮忙
- springmvc要怎么设置才会返回JSON数据 默认情况下springmvc返回的是啥形式的数据啊
- 怎么在服务器上运行同一应用的不同实例?
- 在EJB中客户端是否可以通过本地接口来调用望高手指点
{
private static String MAILBOX=YpwConstants.TM_ENGINE_MAILBOX;
private static String MAILSERVER =YpwConstants.TM_ENGINE_MAILSERVER;
private static String MAIL_HOST =YpwConstants.TM_ENGINE_MAIL_HOST;
Vector recipients = new Vector();
String subject_ = new String();
String message_ = new String();
String fromUsr_ = new String();
String mailBox_ = new String();
String userID_ = new String();
public YpwTmEngineMailService()
{ }
/**
* Send the mail to user.
*/
public void send()
{
String from = fromUsr_ + YpwCode.TM_ENGINE_MAIL_BOX;
String host = YpwCode.TM_ENGINE_MAIL_SERVER;
Properties props = new Properties();
if ( host == null || host.trim().length() == 0 )
{
YpwWarningHandle.getInstance().logYpwError( this.getClass(), YpwConstants.YPW_LOG_TM_ENGINE_MAIL_SERVER_NULL );
return;
}
props.put(MAIL_HOST, host);
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
try
{
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress( from ));
InternetAddress address[] = new InternetAddress[recipients.size()];
for(int i=0;i<recipients.size();i++){
address[i]=new InternetAddress((String)recipients.elementAt(i)
+YpwCode.TM_ENGINE_MAIL_BOX ); } msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject_ );
msg.setSentDate(new Date());
msg.setText(message_);
Transport.send(msg);
}
catch (Exception mex) {
}
}
/**
* The method is used to send the attribute of Mail.
* @param mail a entity of YpwTmMail.
* @return retrun true if the operation is successful.
*/
public boolean doMail (YpwTmMail mail)
{
boolean doMail = false;
message_ = mail.getMessage();
Vector usrID = mail.getUsr();
if( usrID == null || usrID.size() == 0 )
{
YpwWarningHandle.getInstance().logYpwError( this, MessageCode.ERROR_TM_ENGINE_USER_ID_IS_NULL );
return false;
}
for( int i = 0; i < usrID.size(); i ++ )
{
userID_ = ( String )usrID.get( i );
recipients.add( userID_ );
}
subject_ = mail.getSubject( );
mailBox_ = mail.getMailBox( );
fromUsr_ = mail.getFromUser();
send();
return true;
}
}
{
private Vector userId_ = new Vector( );
private String fromUser_ = new String( );
private String message_ = new String( );
private String subject_ = new String( );
private String mailBox_ = new String( );
/**
* <p>Description: This is construct method.</p>
*/
public void YpwMail() {
}
/**
* <p>Description: This method is to set mail box.</p>
* @param mailBox mail box.
*/
public void setMailBox( String mailBox )
{
this.mailBox_ = mailBox;
}
/**
* <p>Description: This method is to get mail box.</p>
* @return String mail box.
*/
public String getMailBox( )
{
return mailBox_;
}
/**
* <p>Description: This method is to set user.</p>
* @param userId user id.
*/
public void setUsr(Vector userId){
this.userId_ = userId;
}
/**
* <p>Description: This method is to set subject.</p>
* @param subject string of subject.
*/
public void setSubject( String subject )
{
this.subject_ = subject;
}
/**
* <p>Description: This method is to set from user.</p>
* @param fromUser from user.
*/
public void setFromUser( String fromUser )
{
this.fromUser_ = fromUser;
}
/**
* <p>Description: This method is to set message.</p>
* @param message contents of message.
*/
public void setMessage( String message )
{
this.message_ = message;
}
/**
* <p>Description: This method is to get subject.</p>
* @return String subject content.
*/
public String getSubject( )
{
return subject_;
}
/**
* <p>Description: This method is to get user.</p>
* @return Vector user information.
*/
public Vector getUsr( )
{
return userId_;
}
/**
* <p>Description: This method is to get from user.</p>
* @return String from user.
*/
public String getFromUser()
{
return fromUser_;
}
/**
* <p>Description: This method is to get message.</p>
* @return String contents of message.
*/
public String getMessage()
{
return message_;
}}
我正在使用。