[Java] 利用MimeMessage send email

利用program send email 不是一件新鮮事. 工作上須要用JAVA+Spring 去做, 在google及研發下, 所得source code 如下.

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

		// Get system properties and append to session.
		Properties properties = System.getProperties();
		properties.setProperty("mail.smtp.host", "192.168.0.1");
		Session session = Session.getDefaultInstance(properties);
			
		MimeMessage mimeMessage=new MimeMessage(session);
		mimeMessage.setFrom(new InternetAddress("[email protected]"));
			mimeMessage.addRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
			mimeMessage.setSubject("Test subject");
			mimeMessage.setContent("Test content", "text/html; charset=utf-8");
			result=true;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

Reference

About C.H. Ling 260 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.