当前位置: 首页>>代码示例>>Java>>正文


Java Message.setSentDate方法代码示例

本文整理汇总了Java中javax.mail.Message.setSentDate方法的典型用法代码示例。如果您正苦于以下问题:Java Message.setSentDate方法的具体用法?Java Message.setSentDate怎么用?Java Message.setSentDate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.mail.Message的用法示例。


在下文中一共展示了Message.setSentDate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: sendTextMail

import javax.mail.Message; //导入方法依赖的package包/类
public boolean sendTextMail(MailSenderInfo mailInfo) {
    MyAuthenticator authenticator = null;
    Properties pro = mailInfo.getProperties();
    if (mailInfo.isValidate()) {
        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
    }
    try {
        Message mailMessage = new MimeMessage(Session.getInstance(pro, authenticator));
        mailMessage.setFrom(new InternetAddress(mailInfo.getFromAddress()));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()));
        mailMessage.setSubject(mailInfo.getSubject());
        mailMessage.setSentDate(new Date());
        mailMessage.setText(mailInfo.getContent());
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
        return false;
    }
}
 
开发者ID:JamesLiAndroid,项目名称:AndroidKillerService,代码行数:21,代码来源:SimpleMailSender.java

示例2: sendAttachMail

import javax.mail.Message; //导入方法依赖的package包/类
public boolean sendAttachMail(MailSenderInfo mailInfo) {
    MyAuthenticator authenticator = null;
    Properties pro = mailInfo.getProperties();
    if (mailInfo.isValidate()) {
        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
    }
    try {
        Message mailMessage = new MimeMessage(Session.getInstance(pro, authenticator));
        mailMessage.setFrom(new InternetAddress(mailInfo.getFromAddress()));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()));
        mailMessage.setSubject(mailInfo.getSubject());
        mailMessage.setSentDate(new Date());
        Multipart multi = new MimeMultipart();
        BodyPart textBodyPart = new MimeBodyPart();
        textBodyPart.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
        multi.addBodyPart(textBodyPart);
        for (String path : mailInfo.getAttachFileNames()) {
            DataSource fds = new FileDataSource(path);
            BodyPart fileBodyPart = new MimeBodyPart();
            fileBodyPart.setDataHandler(new DataHandler(fds));
            fileBodyPart.setFileName(path.substring(path.lastIndexOf("/") + 1));
            multi.addBodyPart(fileBodyPart);
        }
        mailMessage.setContent(multi);
        mailMessage.saveChanges();
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
        return false;
    }
}
 
开发者ID:JamesLiAndroid,项目名称:AndroidKillerService,代码行数:33,代码来源:SimpleMailSender.java

示例3: sendEmailWithOrder

import javax.mail.Message; //导入方法依赖的package包/类
public static void sendEmailWithOrder(String text, String eMail, HttpServletRequest request) {
    try {
        Session session = EmailActions.authorizeWebShopEmail();

        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(ApplicationProperties.SHOP_EMAIL));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(eMail, false));
        msg.setSubject("Shop order");
        msg.setText(text);
        msg.setSentDate(new Date());
        Transport.send(msg);
    } catch (MessagingException e) {
        System.out.println("Error : " + e);
    }
}
 
开发者ID:xSzymo,项目名称:Spring-web-shop-project,代码行数:16,代码来源:SendEmailUserAccount.java

示例4: sendCode

import javax.mail.Message; //导入方法依赖的package包/类
public static String sendCode(User user, HttpServletRequest request) {
    try {
        Session session = EmailActions.authorizeWebShopEmail();

        String code = Long.toHexString(Double.doubleToLongBits(Math.random()));
        request.getSession().setAttribute("deleteAccountCode", code);
        request.getSession().setAttribute("userName", user.getLogin());
        System.out.println(code);

        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(ApplicationProperties.SHOP_EMAIL));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(user.geteMail(), false));
        msg.setSubject("Delete account");
        msg.setText("Link : " + ApplicationProperties.URL + ApplicationProperties.PROJECT_NAME + "account/deleteAccountCode/" + code);
        msg.setSentDate(new Date());
        Transport.send(msg);

    } catch (MessagingException e) {
        System.out.println("Error : " + e);
    }
    return "loginAndRegistration/reset/codePassword";
}
 
开发者ID:xSzymo,项目名称:Spring-web-shop-project,代码行数:23,代码来源:SendEmailDeleteAccount.java

示例5: sendHtmlMail

import javax.mail.Message; //导入方法依赖的package包/类
public boolean sendHtmlMail(MailSenderInfo mailInfo) {
    XLog.d("发送网页版邮件!");
    MyAuthenticator authenticator = null;
    Properties pro = mailInfo.getProperties();
    if (mailInfo.isValidate()) {
        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
    }
    try {
        Message mailMessage = new MimeMessage(Session.getInstance(pro, authenticator));
        mailMessage.setFrom(new InternetAddress(mailInfo.getFromAddress()));
        String[] receivers = mailInfo.getReceivers();
        Address[] tos = new InternetAddress[receivers.length];
        for (int i = 0; i < receivers.length; i++) {
            tos[i] = new InternetAddress(receivers[i]);
        }
        mailMessage.setRecipients(Message.RecipientType.TO, tos);
        mailMessage.setSubject(mailInfo.getSubject());
        mailMessage.setSentDate(new Date());

        Multipart mainPart = new MimeMultipart();
        BodyPart html = new MimeBodyPart();
        html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        mailMessage.setContent(mainPart);
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
        return false;
    }
}
 
开发者ID:JamesLiAndroid,项目名称:AndroidKillerService,代码行数:32,代码来源:SimpleMailSender.java

示例6: sendEmail

import javax.mail.Message; //导入方法依赖的package包/类
public static void sendEmail(String host, String port,
        final String userName, final String password, String toAddress,
        String subject, String message, String nombreArchivoAdj, String urlArchivoAdj)
        		throws AddressException, MessagingException {
 
    // sets SMTP server properties
    Properties properties = new Properties();
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.port", port);
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
 
    // creates a new session with an authenticator
    Authenticator auth = new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
    };
 
    Session session = Session.getInstance(properties, auth);

    //Se crea la parte del cuerpo del mensaje.
    BodyPart texto = new MimeBodyPart();
    texto.setText(message);        
    BodyPart adjunto = new MimeBodyPart();
    
    if(nombreArchivoAdj != null ){	        
     adjunto.setDataHandler(new DataHandler(new FileDataSource(urlArchivoAdj)));
     adjunto.setFileName(nombreArchivoAdj);
    }
    
    //Juntar las dos partes
    MimeMultipart multiParte = new MimeMultipart();
    multiParte.addBodyPart(texto);
    if (nombreArchivoAdj != null) multiParte.addBodyPart(adjunto);
    
    // creates a new e-mail message
    Message msg = new MimeMessage(session);
 
    msg.setFrom(new InternetAddress(userName));
    InternetAddress[] toAddresses = null;
    toAddresses = InternetAddress.parse(toAddress, false);

    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    //msg.setText(message);
    msg.setContent(multiParte);
 
    // sends the e-mail
    Transport.send(msg);       

}
 
开发者ID:stppy,项目名称:spr,代码行数:54,代码来源:SendMail.java

示例7: _setupMessage

import javax.mail.Message; //导入方法依赖的package包/类
/**
 * Set up a new message.
 */
private Message _setupMessage(Message msg)
{
  try
  {
    String username = _account.getUsername();
    String from = username + "@" + _account.getDomain();
    List<InternetAddress> to = _getEmailList(getTo());
    
    List<InternetAddress> cc = null;
    String ccString = getCc();
    if(ccString != null) 
    {
      cc = _getEmailList(ccString);  
    }
    
    msg.setFrom(new InternetAddress(from));
    if ((to != null) && !to.isEmpty())
      msg.setRecipients(Message.RecipientType.TO,
                        to.toArray(new InternetAddress[0]));

    if ((cc != null) && !cc.isEmpty())
      msg.setRecipients(Message.RecipientType.CC,
                        cc.toArray(new InternetAddress[0]));
    msg.setSubject(_subject == null ? "" : _subject);
    if ((_attachment1 == null) &&
        (_attachment2 == null) &&
        (_attachment3 == null))
    {
      msg.setText(_content == null ? "" : _content);
    }
    // Multipart.
    else
    {
      // Create the message part
      BodyPart messageBodyPart = new MimeBodyPart();

      // Fill the message
      messageBodyPart.setText(_content == null ? "" : _content);

      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(messageBodyPart);

      if (_attachment1 != null)
        _addAttachment(multipart, _attachment1);
      if (_attachment2 != null)
        _addAttachment(multipart, _attachment2);
      if (_attachment3 != null)
        _addAttachment(multipart, _attachment3);

      // Put all the parts in the message
      msg.setContent(multipart);
    }

    String mailer = "OracleAdfEmailDemo";
    msg.setHeader("X-Mailer", mailer);
    msg.setSentDate(new Date());

    return msg;
  }
  catch(AddressException ae)
  {
     _showSendException(ae);
  }
  catch(MessagingException me)
  {
    _showSendException(me);
  }
  catch(Exception e)
  {
    _showSendException(e);
  }

  return null;
}
 
开发者ID:apache,项目名称:myfaces-trinidad,代码行数:78,代码来源:NewMessageBackingBean.java

示例8: sendHtmlMail

import javax.mail.Message; //导入方法依赖的package包/类
/**
 * 以HTML格式发送邮件
 * 
 * @param mailInfo
 *            待发送的邮件信息
 */
public boolean sendHtmlMail(MailSenderObj mailInfo) {
	// 判断是否需要身份认证
	MyAuthenticator authenticator = null;
	Properties pro = mailInfo.getProperties();
	// 如果需要身份认证,则创建一个密码验证器
	if (mailInfo.isValidate()) {
		authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
	}
	// 根据邮件会话属性和密码验证器构造一个发送邮件的session
	Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
	sendMailSession.setDebug(true);// 设置debug模式 在控制台看到交互信息
	try {
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(sendMailSession);
		// 创建邮件发送者地址
		Address from = new InternetAddress(mailInfo.getFromAddress());
		// 设置邮件消息的发送者
		mailMessage.setFrom(from);
		// 创建邮件的接收者地址,并设置到邮件消息中
		String[] asToAddr = mailInfo.getToAddress();
		if (asToAddr == null) {
			logger.debug("邮件发送失败,收信列表为空" + mailInfo);
			return false;
		}
		for (int i=0; i<asToAddr.length; i++) {
			if (asToAddr[i] == null || asToAddr[i].equals("")) {
				continue;
			}
			Address to = new InternetAddress(asToAddr[i]);
			// Message.RecipientType.TO属性表示接收者的类型为TO
			mailMessage.addRecipient(Message.RecipientType.TO, to);
		}
		// 设置邮件消息的主题
		mailMessage.setSubject(mailInfo.getSubject());
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(new Date());
		// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
		Multipart mainPart = new MimeMultipart();
		// 创建一个包含HTML内容的MimeBodyPart
		BodyPart html = new MimeBodyPart();
		// 设置HTML内容
		html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
		mainPart.addBodyPart(html);
		// 将MiniMultipart对象设置为邮件内容
		mailMessage.setContent(mainPart);
		// 发送邮件
		Transport.send(mailMessage);
		logger.info("发送邮件成功。" + mailInfo);
		return true;
	} catch (MessagingException ex) {
		logger.error("发送邮件失败:" + ex.getMessage() + Arrays.toString(ex.getStackTrace()));
	}
	return false;
}
 
开发者ID:langxianwei,项目名称:iot-plat,代码行数:61,代码来源:SimpleMailSender.java

示例9: sendCode

import javax.mail.Message; //导入方法依赖的package包/类
@RequestMapping(value = "sendCode", method = RequestMethod.POST)
public String sendCode(@RequestParam("login") String login, @RequestParam("email") String email, Model model,
                       HttpServletResponse response, HttpServletRequest request) {

    if ((usersService.findByEmail(email) == null)) {
        model.addAttribute("msg", "Wrong e-mail");
        return "loginAndRegistration/reset/forgotPassword";
    } else if (usersService.findByLogin(login) == null) {
        model.addAttribute("msg", "Wrong login");
        return "loginAndRegistration/reset/forgotPassword";
    }

    try {
        Session session = EmailActions.authorizeWebShopEmail();

        String code = Long.toHexString(Double.doubleToLongBits(Math.random()));
        request.getSession().setAttribute("code", code);
        request.getSession().setAttribute("email", email);

        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(ApplicationProperties.SHOP_EMAIL));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email, false));
        msg.setSubject("Reset password");
        msg.setText("After 5 min code will be delete\n" + code);
        msg.setSentDate(new Date());
        Transport.send(msg);

    } catch (MessagingException e) {
        System.out.println("Error : " + e);
    }
    return "loginAndRegistration/reset/codePassword";
}
 
开发者ID:xSzymo,项目名称:Spring-web-shop-project,代码行数:33,代码来源:SendEmailForgetPasswordLogin.java

示例10: sendUsername

import javax.mail.Message; //导入方法依赖的package包/类
@RequestMapping(value = "sendUsername", method = RequestMethod.POST)
public String sendUsername(@RequestParam("email") String email, Model model, HttpServletResponse response,
                           HttpServletRequest request) {

    if ((usersService.findByEmail(email) == null)) {
        model.addAttribute("msg", "Wrong e-mail");
        return "loginAndRegistration/reset/forgotUsername";
    }
    User user = usersService.findByEmail(email);

    try {
        Session session = EmailActions.authorizeWebShopEmail();

        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(ApplicationProperties.SHOP_EMAIL));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email, false));
        msg.setSubject("Your login");
        msg.setText("Login : " + user.getLogin());
        msg.setSentDate(new Date());
        Transport.send(msg);
    } catch (MessagingException e) {
        System.out.println("Error : " + e);
    }

    model.addAttribute("msg", "Success");

    return "loginAndRegistration/reset/forgotUsername";
}
 
开发者ID:xSzymo,项目名称:Spring-web-shop-project,代码行数:29,代码来源:SendEmailForgetPasswordLogin.java

示例11: emailPassword

import javax.mail.Message; //导入方法依赖的package包/类
private boolean emailPassword(String password, String username, String email) {
    /* Default to protocol smtp when SmtpEncryption is set to "None" */
    String prot = "smtp";

    if (settingsService.getSmtpServer() == null || settingsService.getSmtpServer().isEmpty()) {
        LOG.warn("Can not send email; no Smtp server configured.");
        return false;
    }

    Properties props = new Properties();
    if (settingsService.getSmtpEncryption().equals("SSL/TLS")) {
        prot = "smtps";
        props.put("mail." + prot + ".ssl.enable", "true");
    } else if (settingsService.getSmtpEncryption().equals("STARTTLS")) {
        prot = "smtp";
        props.put("mail." + prot + ".starttls.enable", "true");
    }
    props.put("mail." + prot + ".host", settingsService.getSmtpServer());
    props.put("mail." + prot + ".port", settingsService.getSmtpPort());
    /* use authentication when SmtpUser is configured */
    if (settingsService.getSmtpUser() != null && !settingsService.getSmtpUser().isEmpty()) {
        props.put("mail." + prot + ".auth", "true");
    }

    Session session = Session.getInstance(props, null);

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(settingsService.getSmtpFrom()));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
        message.setSubject("Airsonic Password");
        message.setText("Hi there!\n\n" +
                "You have requested to reset your Airsonic password.  Please find your new login details below.\n\n" +
                "Username: " + username + "\n" +
                "Password: " + password + "\n\n" +
                "--\n" +
                "Your Airsonic server\n" +
                "airsonic.github.io/");
        message.setSentDate(new Date());

        Transport trans = session.getTransport(prot);
        try {
            if (props.get("mail." + prot + ".auth") != null && props.get("mail." + prot + ".auth").equals("true")) {
                trans.connect(settingsService.getSmtpServer(), settingsService.getSmtpUser(), settingsService.getSmtpPassword());
            } else {
                trans.connect();
            }
            trans.sendMessage(message, message.getAllRecipients());
        } finally {
            trans.close();
        }
        return true;

    } catch (Exception x) {
        LOG.warn("Failed to send email.", x);
        return false;
    }
}
 
开发者ID:airsonic,项目名称:airsonic,代码行数:59,代码来源:RecoverController.java

示例12: sendMail

import javax.mail.Message; //导入方法依赖的package包/类
public boolean sendMail(String to,String otp) throws MessagingException 
{
	String host="smtp.gmail.com";
	String username="";//emailid
	String password="";//emailid password
	String from=" ";// email from which u have to send
	String subject="One Time Password";
	String body="Your One Time passsword is "+otp;
	
	boolean sessionDebug=false;
	
	Properties props=System.getProperties();
	props.put("mail.host",host);
	props.put("mail.transport.protocol","smtp");
	props.put("mail.smtp.starttls.enable","true");
	props.put("mail.smtp.auth", "true");
	props.put("mail.smtp.debug", "true");
	props.put("mail.smtp.socketFactory.port", "465");
	props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	props.put("mail.smtp.socketFactory.fallback", "false");
	props.put("mail.smtp.host", "smtp.gmail.com");
	props.put("mail.smtp.port", "25"); 
	
	Session mailSession=Session.getDefaultInstance(props,null);
	mailSession.setDebug(sessionDebug);
	
	Message msg=new MimeMessage(mailSession);
	msg.setFrom(new InternetAddress(from));
	InternetAddress [] address={new InternetAddress(to)};
	msg.setRecipients(Message.RecipientType.TO,address);
	msg.setSubject(subject);
	msg.setSentDate(new Date());
	msg.setText(body);

	Transport tr=mailSession.getTransport("smtp");
	tr.connect(host,username,password);
	msg.saveChanges();
	tr.sendMessage(msg,msg.getAllRecipients());
	tr.close();
	//Transport.send(msg);
	return true;
}
 
开发者ID:nishittated,项目名称:OnlineElectionVotingSystem,代码行数:43,代码来源:PasswordMail.java

示例13: sendMail1

import javax.mail.Message; //导入方法依赖的package包/类
public boolean sendMail1(String to,String passwrd, String link) throws MessagingException
{
	String host="smtp.gmail.com";
	String username="	 ";//emailid
	String password="";//emailid password
	String from=" ";// email from which u have to send
	String subject="Website Password";
	String body="Your website password is "+passwrd+"  Please click to reset "+link;
	boolean sessionDebug=false;
	
	Properties props=System.getProperties();
	props.put("mail.host",host);
	props.put("mail.transport.protocol","smtp");
	props.put("mail.smtp.starttls.enable","true");
	props.put("mail.smtp.auth", "true");
	props.put("mail.smtp.debug", "true");
	props.put("mail.smtp.socketFactory.port", "465");
	props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	props.put("mail.smtp.socketFactory.fallback", "false");
	props.put("mail.smtp.host", "smtp.gmail.com");
	props.put("mail.smtp.port", "25"); 
	
	Session mailSession=Session.getDefaultInstance(props,null);
	mailSession.setDebug(sessionDebug);

	Message msg=new MimeMessage(mailSession);
	msg.setFrom(new InternetAddress(from));
	
	InternetAddress [] address={new InternetAddress(to)};
	msg.setRecipients(Message.RecipientType.TO,address);
	msg.setSubject(subject);
	msg.setSentDate(new Date());
	msg.setText(body);
	
	Transport tr=mailSession.getTransport("smtp");
	tr.connect(host,username,password);
	msg.saveChanges();
	tr.sendMessage(msg,msg.getAllRecipients());
	tr.close();
			//Transport.send(msg);
	return true;
}
 
开发者ID:nishittated,项目名称:OnlineElectionVotingSystem,代码行数:43,代码来源:PasswordMail.java

示例14: sendMail2

import javax.mail.Message; //导入方法依赖的package包/类
public boolean sendMail2(String to, String link) throws AddressException, MessagingException {

		String host="smtp.gmail.com";
		String username="	 ";//emailid
		String password="";//emailid password
		String from=" ";// email from which u have to send
		String subject="Website Password";
		String body="Activation Link"+link+"Please click to reset "+link;
		boolean sessionDebug=false;
		
		Properties props=System.getProperties();
		props.put("mail.host",host);
		props.put("mail.transport.protocol","smtp");
		props.put("mail.smtp.starttls.enable","true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.debug", "true");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "25"); 
		
		Session mailSession=Session.getDefaultInstance(props,null);
		mailSession.setDebug(sessionDebug);
	
		Message msg=new MimeMessage(mailSession);
		msg.setFrom(new InternetAddress(from));
	
		InternetAddress [] address={new InternetAddress(to)};
		msg.setRecipients(Message.RecipientType.TO,address);
		msg.setSubject(subject);
		msg.setSentDate(new Date());
		msg.setText(body);
	
		Transport tr=mailSession.getTransport("smtp");
		tr.connect(host,username,password);
		msg.saveChanges();
		tr.sendMessage(msg,msg.getAllRecipients());
		tr.close();
			//Transport.send(msg);
		return true;
	}
 
开发者ID:nishittated,项目名称:OnlineElectionVotingSystem,代码行数:43,代码来源:PasswordMail.java

示例15: sendTextMail

import javax.mail.Message; //导入方法依赖的package包/类
/**
 * 以文本格式发送邮件
 * 
 * @param mailInfo
 *            待发送的邮件的信息
 */
public boolean sendTextMail(MailSenderObj mailInfo) {
	// 判断是否需要身份认证
	MyAuthenticator authenticator = null;
	Properties pro = mailInfo.getProperties();
	if (mailInfo.isValidate()) {
		// 如果需要身份认证,则创建一个密码验证器
		authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
	}
	// 根据邮件会话属性和密码验证器构造一个发送邮件的session
	Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
	try {
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(sendMailSession);
		// 创建邮件发送者地址
		Address from = new InternetAddress(mailInfo.getFromAddress());
		// 设置邮件消息的发送者
		mailMessage.setFrom(from);
		// 创建邮件的接收者地址,并设置到邮件消息中
		String[] asToAddr = mailInfo.getToAddress();
		if (asToAddr == null) {
			logger.debug("邮件发送失败,收信列表为空" + mailInfo);
			return false;
		}
		for (int i=0; i<asToAddr.length; i++) {
			if (asToAddr[i] == null || asToAddr[i].equals("")) {
				continue;
			}
			Address to = new InternetAddress(asToAddr[i]);
			mailMessage.addRecipient(Message.RecipientType.TO, to);
		}
		// 设置邮件消息的主题
		mailMessage.setSubject(mailInfo.getSubject());
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(new Date());
		// 设置邮件消息的主要内容
		String mailContent = mailInfo.getContent();
		mailMessage.setText(mailContent);
		// 发送邮件
		Transport.send(mailMessage);
		logger.info("发送邮件成功。" + mailInfo);
		return true;
	} catch (MessagingException ex) {
		logger.error("发送邮件失败:" + ex.getMessage() + Arrays.toString(ex.getStackTrace()));
		ex.printStackTrace();
	}
	return false;
}
 
开发者ID:langxianwei,项目名称:iot-plat,代码行数:54,代码来源:SimpleMailSender.java


注:本文中的javax.mail.Message.setSentDate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。