[Spring MVC] 如何發出電郵

public boolean sendMail(EmailMessage emailMessage) {
		boolean result=false;
		try {
			// Generate Spring MIME message and send.

		      // Get system properties
			Properties properties = System.getProperties();

			if(ServerUtil.isWindows())
				properties.setProperty(EMAIL_SMTP_HOSTNAME_WIN, smtpHostName);
			else
				properties.setProperty(EMAIL_SMTP_HOSTNAME_LINUX, smtpHostName);
			
			Session session = Session.getDefaultInstance(properties);
			
			MimeMessage mimeMessage=new MimeMessage(session);
			//MimeMessage mimeMessage=javaMailSender.createMimeMessage();
			mimeMessage.setFrom(new InternetAddress(emailMessage.getSender()));
			mimeMessage.addRecipient(RecipientType.TO, new InternetAddress(emailMessage.getReceiver()));
			mimeMessage.setSubject(emailMessage.getSubject());
			mimeMessage.setContent(emailMessage.getContent(), "text/html; charset=utf-8");
			Transport.send(mimeMessage);
			//javaMailSender.send(mimeMessage);
			
			result=true;
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.getMessage());
		}
		return result;
	}

 

About C.H. Ling 262 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.