{
boolean flag = true;
//建立邮件会话
Properties pro = new Properties();
proput("mailsmtphost","smtpqqcom");//存储发送邮件的服务器
proput("mailsmtpauth","true"); //通过服务器验证
Session s =SessiongetInstance(pro); //根据属性新建一个邮件会话
//ssetDebug(true);
//由邮件会话新建一个消息对象
MimeMessage message = new MimeMessage(s);
//设置邮件
InternetAddress fromAddr = null;
InternetAddress toAddr = null;
try
{
fromAddr = new InternetAddress(451144426+"@qqcom"); //邮件发送地址
messagesetFrom(fromAddr); //设置发送地址
toAddr = new InternetAddress("12345367@qqcom"); //邮件接收地址
messagesetRecipient(MessageRecipientTypeTO, toAddr); //设置接收地址
messagesetSubject(title); //设置邮件标题
messagesetText(content); //设置邮件正文
messagesetSentDate(new Date()); //设置邮件日期
messagesaveChanges(); //保存邮件更改信息
Transport transport = sgetTransport("smtp");
transportconnect("smtpqqcom", "451144426", "密码"); //服务器地址,邮箱账号,邮箱密码
transportsendMessage(message, messagegetAllRecipients()); //发送邮件
transportclose();//关闭
}
catch (Exception e)
{
eprintStackTrace();
flag = false;//发送失败
}
return flag;
}
这是一个javaMail的邮件发送代码,需要一个mailjarimport javautil;
import javaxmail;import javaxmailinternet;
public class JMail {
public void SendMail(String Topic,String Content){ Properties props=new Properties(); propsput("mailsmtphost","smtp163com"); propsput("mailsmtpauth","true"); Session s=SessiongetInstance(props); ssetDebug(false); MimeMessage message=new MimeMessage(s); MimeMultipart mp=new MimeMultipart(); BodyPart body = new MimeBodyPart(); InternetAddress from; InternetAddress to; try{ from=new InternetAddress("发件人邮箱"); messagesetFrom(from); to = new InternetAddress("收件人邮箱"); messagesetRecipient(MessageRecipientTypeTO,to); messagesetSubject(Topic,"utf-8"); bodysetContent(Content, "text/html;charset=utf-8"); mpaddBodyPart(body); messagesetContent(mp); messagesetSentDate(new Date()); messagesaveChanges(); Transport transport=sgetTransport("smtp"); transportconnect("smtp163com(邮件服务商,这是163的)","发件邮箱","发件邮箱密码"); transportsendMessage(message,messagegetAllRecipients()); transportclose(); } catch(AddressException e){ eprintStackTrace(); } catch(MessagingException e){ eprintStackTrace(); } }}要实现邮件发送功能需要导入包:mailjar
/
Generated by MyEclipse Struts
Template path: templates/java/JavaClassvtl
/
package orgdemoaction;
import javautilProperties;
import javaxmailMessage;
import javaxmailSession;
import javaxmailTransport;
import javaxmailinternetInternetAddress;
import javaxmailinternetMimeMessage;
import javaxservlet>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)