Java Code Examples for org.springframework.mail.SimpleMailMessage#setSubject()

The following examples show how to use org.springframework.mail.SimpleMailMessage#setSubject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SendMail.java    From shark with Apache License 2.0 6 votes vote down vote up
public SendMail() {
	mailBean = new MailBean();
	mailSender = new JavaMailSenderImpl();
	mailSender.setPort(mailBean.getPORT());
	mailSender.setHost(mailBean.getHOST());
	mailSender.setUsername(mailBean.getUSERNAME());
	mailSender.setPassword(mailBean.getPASSWORD());
	Properties properties = new Properties();
	properties.setProperty("mail.smtp.auth", "true");
	mailSender.setJavaMailProperties(properties);
	mailMessage = new SimpleMailMessage();
	mailMessage.setFrom(mailBean.getFROM());
	mailMessage.setTo(mailBean.getTO());
	mailMessage.setSubject(mailBean.getSUBJECT());
	GetUserInfo getUserInfo = new GetUserInfo();
	mailMessage.setText("[version]:" + LoadVersion.getVersion() + "\n[javaVersion]:" + getUserInfo.getJavaVersion()
			+ "\n[osName]:" + getUserInfo.getOsName() + "\n[jvmName]:" + getUserInfo.getJvmName() + "\n[address]:"
			+ getUserInfo.getAddress() + "\n[hostName]:" + getUserInfo.getHostname() + "\n[startTime]:"
			+ getUserInfo.getStartTime());
}
 
Example 2
Source File: EmailMessageSender.java    From magic-starter with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 处理简单邮件类型
 *
 * @param message 邮件内容
 * @return boolean
 */
private boolean processSimpleEmail(EmailMessage message) {
	// 注意邮件发送可能出现异常,注意catch
	try {
		SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
		simpleMailMessage.setFrom("\"" + message.getFrom() + "\" <" + username + ">");
		simpleMailMessage.setTo(ArrayUtil.toArray(message.getTos(), String.class));
		simpleMailMessage.setSubject(message.getSubject());
		simpleMailMessage.setText(message.getContent());

		// 设置抄送人列表
		if (CollUtil.isEmpty(message.getCcs())) {
			simpleMailMessage.setCc(ArrayUtil.toArray(message.getCcs(), String.class));
		}
		mailSender.send(simpleMailMessage);
		return true;
	} catch (Exception e) {
		log.error("简单邮件发送异常!", e);
		return false;
	}
}
 
Example 3
Source File: MailerImpl.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
    * Construct and a send simple email message from a Mail Message.
    * 
    * @param message
    *            the Mail Message
 * @throws MessagingException 
    */
@Override
      @SuppressWarnings("unchecked")
public void sendEmail(MailMessage message) throws MessagingException {
       
       // Construct a simple mail message from the Mail Message
       SimpleMailMessage smm = new SimpleMailMessage();
       smm.setTo( (String[])message.getToAddresses().toArray(new String[message.getToAddresses().size()]) );
       smm.setBcc( (String[])message.getBccAddresses().toArray(new String[message.getBccAddresses().size()]) );
       smm.setCc( (String[])message.getCcAddresses().toArray(new String[message.getCcAddresses().size()]) );
       smm.setSubject(message.getSubject());
       smm.setText(message.getMessage());
       smm.setFrom(message.getFromAddress());

       try {
       	if ( LOG.isDebugEnabled() ) {
       		LOG.debug( "sendEmail() - Sending message: " + smm.toString() );
       	}
           mailSender.send(smm);
       }
       catch (Exception e) {
       	LOG.error("sendEmail() - Error sending email.", e);
		throw new RuntimeException(e);
       }
   }
 
Example 4
Source File: MailServiceImpl.java    From DouBiNovel with Apache License 2.0 5 votes vote down vote up
@Override
public void sendSimpleMail(String to, String subject, String content) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from);
    message.setTo(to);
    message.setSubject(subject);
    message.setText(content);
    try {
        mailSender.send(message);
        logger.info("简单邮件已经发送。");
    } catch (Exception e) {
        logger.error("发送简单邮件时发生异常!", e);
    }
}
 
Example 5
Source File: EmailServiceImpl.java    From tutorials with MIT License 5 votes vote down vote up
public void sendSimpleMessage(String to, String subject, String text) {
    try {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(NOREPLY_ADDRESS);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);

        emailSender.send(message);
    } catch (MailException exception) {
        exception.printStackTrace();
    }
}
 
Example 6
Source File: EmailController.java    From Spring-MVC-Blueprints with MIT License 5 votes vote down vote up
public void sendSimpleMail(Email emailForm) {
	SimpleMailMessage message = new SimpleMailMessage();
	message.setTo(emailForm.getSendTo());
	message.setSubject(emailForm.getSubject());
	message.setText(emailForm.getMessage());
	emailSender.send(message);
}
 
Example 7
Source File: EmailBuilder.java    From zheshiyigeniubidexiangmu with MIT License 5 votes vote down vote up
/**
 * 发送简单邮件
 * @param from 发件人
 * @param to 收件人
 * @param subject 主题
 * @param text 正文
 */
public void sendSimpleEmail(String from, String to, String subject, String text){
    SimpleMailMessage message = new SimpleMailMessage();//消息构造器
    message.setFrom(from);//发件人
    message.setTo(to);//收件人
    message.setSubject(subject);//主题
    message.setText(text);//正文
    this.send(message);
}
 
Example 8
Source File: MailSender.java    From restfiddle with Apache License 2.0 5 votes vote down vote up
public void sendMail(String from, String to, String subject, String msg) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(msg);

javaMailSender.send(message);
   }
 
Example 9
Source File: MailServiceImpl.java    From spring-cloud-sofastack-samples with Apache License 2.0 5 votes vote down vote up
/**
 * 发送文本邮件
 *
 * @param toAddr
 * @param title
 * @param content
 */
@Override
public void sendTextMail(String toAddr, String title, String content) {
    // 纯文本邮件对象
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from);
    message.setTo(toAddr);
    message.setSubject(title);
    message.setText(content);
    try {
        mailSender.send(message);
    } catch (Exception e) {
        logger.error("发送邮件时发生异常!", e);
    }
}
 
Example 10
Source File: EmailSender.java    From Spring-Boot-Examples with MIT License 5 votes vote down vote up
void sendEmail(String to, String from, String title, String content) {
    SimpleMailMessage mail = new SimpleMailMessage();
    mail.setTo(to);
    mail.setFrom(from);
    mail.setSubject(title);
    mail.setText(content);
    javaMailSender.send(mail);
}
 
Example 11
Source File: SendEmailService.java    From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void sendEmail(JavaMailSenderImpl javaMailSenderImpl, String from, String to, String subject, String body) {
	SimpleMailMessage mailMessage = new SimpleMailMessage();
	mailMessage.setFrom(from);
	mailMessage.setTo(to);
	mailMessage.setSubject(subject);
	mailMessage.setText(body);
	javaMailSenderImpl.send(mailMessage);
}
 
Example 12
Source File: MailServiceImpl.java    From mySpringBoot with Apache License 2.0 5 votes vote down vote up
/**
 * 发送简单邮件
 */
@Override
public void sendSimpleMail(Mail mail){
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from);
    message.setTo(mail.getTo());
    message.setSubject(mail.getSubject());
    message.setText(mail.getText());
    message.setCc(mail.getCc());
    mailSender.send(message);
}
 
Example 13
Source File: MailServiceImpl.java    From nimrod with MIT License 5 votes vote down vote up
private void sendSimpleMailMessage(String from, String[] toArray, String subject, String text) {
    SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
    simpleMailMessage.setFrom(from);
    simpleMailMessage.setTo(toArray);
    simpleMailMessage.setSubject(subject);
    simpleMailMessage.setText(text);
    javaMailSender.send(simpleMailMessage);
}
 
Example 14
Source File: MailService.java    From spring-boot-101 with Apache License 2.0 5 votes vote down vote up
/**
 * 发送纯文本的简单邮件
 */
public void sendSimpleMail(String to, String subject, String content){
	SimpleMailMessage message = new SimpleMailMessage();
	message.setFrom(from);
	message.setTo(to);
	message.setSubject(subject);
	message.setText(content);

	try {
		sender.send(message);
		logger.info("简单邮件已经发送。");
	} catch (Exception e) {
		logger.error("发送简单邮件时发生异常!", e);
	}
}
 
Example 15
Source File: MailSender.java    From sso with MIT License 5 votes vote down vote up
@Override
public void send(MailInformative informative) throws ValidateSenderException {
    SimpleMailMessage message = new SimpleMailMessage();

    message.setFrom(informative.getFromMail());//发送者.
    message.setTo(informative.getToMail());//接收者.
    message.setSubject(informative.getSubject());//邮件主题.
    message.setText(informative.getContent());//邮件内容.

    mailSender.send(message);//发送邮件
}
 
Example 16
Source File: NotifyService.java    From BigDataPlatform with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 邮件消息通知,
 * 接收者在spring.mail.sendto中指定
 *
 * @param subject 邮件标题
 * @param content 邮件内容
 */
@Async
public void notifyMail(String subject, String content) {
    if (mailSender == null)
        return;

    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(sendFrom);
    message.setTo(sendTo);
    message.setSubject(subject);
    message.setText(content);
    mailSender.send(message);
}
 
Example 17
Source File: MailHelper.java    From fiery with Apache License 2.0 5 votes vote down vote up
public void sendSimpleMail(String from, String to, String subject, String content) throws Exception {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from);
    message.setTo(to);
    message.setSubject(subject);
    message.setText(content);
    mailSender.send(message);
}
 
Example 18
Source File: MailTool.java    From maintain with MIT License 5 votes vote down vote up
public static void sendMail(JavaMailSender mailSender, String from, String to, String subject, String text) {
	try {
		
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(from);// 发送者.
		message.setTo(to);// 接收者.
		message.setSubject(subject);// 邮件主题.
		message.setText(text);// 邮件内容.

		mailSender.send(message);// 发送邮件
		
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 19
Source File: JavaMailSenderTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void javaMailSenderWithSimpleMessage() throws MessagingException, IOException {
	MockJavaMailSender sender = new MockJavaMailSender();
	sender.setHost("host");
	sender.setPort(30);
	sender.setUsername("username");
	sender.setPassword("password");

	SimpleMailMessage simpleMessage = new SimpleMailMessage();
	simpleMessage.setFrom("[email protected]");
	simpleMessage.setReplyTo("[email protected]");
	simpleMessage.setTo("[email protected]");
	simpleMessage.setCc("[email protected]", "[email protected]");
	simpleMessage.setBcc("[email protected]", "[email protected]");
	Date sentDate = new GregorianCalendar(2004, 1, 1).getTime();
	simpleMessage.setSentDate(sentDate);
	simpleMessage.setSubject("my subject");
	simpleMessage.setText("my text");
	sender.send(simpleMessage);

	assertEquals("host", sender.transport.getConnectedHost());
	assertEquals(30, sender.transport.getConnectedPort());
	assertEquals("username", sender.transport.getConnectedUsername());
	assertEquals("password", sender.transport.getConnectedPassword());
	assertTrue(sender.transport.isCloseCalled());

	assertEquals(1, sender.transport.getSentMessages().size());
	MimeMessage sentMessage = sender.transport.getSentMessage(0);
	List<Address> froms = Arrays.asList(sentMessage.getFrom());
	assertEquals(1, froms.size());
	assertEquals("[email protected]", ((InternetAddress) froms.get(0)).getAddress());
	List<Address> replyTos = Arrays.asList(sentMessage.getReplyTo());
	assertEquals("[email protected]", ((InternetAddress) replyTos.get(0)).getAddress());
	List<Address> tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO));
	assertEquals(1, tos.size());
	assertEquals("[email protected]", ((InternetAddress) tos.get(0)).getAddress());
	List<Address> ccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.CC));
	assertEquals(2, ccs.size());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(1)).getAddress());
	List<Address> bccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.BCC));
	assertEquals(2, bccs.size());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(1)).getAddress());
	assertEquals(sentDate.getTime(), sentMessage.getSentDate().getTime());
	assertEquals("my subject", sentMessage.getSubject());
	assertEquals("my text", sentMessage.getContent());
}
 
Example 20
Source File: Application.java    From spring-boot with MIT License 3 votes vote down vote up
void sendEmail() {

        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setTo("[email protected]", "[email protected]");

        msg.setSubject("Testing from Spring Boot");
        msg.setText("Hello World \n Spring Boot Email");

        javaMailSender.send(msg);

    }