统一textarea在IE、Firefox下的效果
Ajax 获取JSON数据

使用JavaMail SMTP协议发送邮件

Jimmy posted @ 2012年4月08日 00:42 in JSP+MySQL , 12674 阅读

最近需要实现通过发送邮件让用户找回密码的功能,自己用Socket写了SMTP协议的邮件发送程序,但是很多邮件服务器的anti-spam需要验证发送邮箱的合法性,所以只得放弃,后来发现用javamail包可以很方便的实现。示例程序使用gmail的邮件服务器来发送邮件。关于SMTP端口等配置见下面链接:

https://support.google.com/mail/bin/answer.py?hl=en&answer=13287

注:下面程序需导入javaee-api-6.0.jar 跟 mail.jar

1.使用TLS发送邮件

import java.util.Properties;
 
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class SendMailTLS {
 
	public static void main(String[] args) {
 
		final String username = "someoe@gmail.com";
		final String password = "password";
 
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
 
		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });
 
		try {
 
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("someone@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse("someone@yourISP.com"));
			message.setSubject("Testing Subject");
			message.setText("Dear Mail Crawler,"
				+ "\n\n No spam to my email, please!");
 
			Transport.send(message);
 
			System.out.println("Done");
 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}

2.使用SSL发送邮件

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class SendMailSSL {
	public static void main(String[] args) {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
 
		Session session = Session.getDefaultInstance(props,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("username","password");
				}
			});
 
		try {
 
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("from@no-spam.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse("to@no-spam.com"));
			message.setSubject("Testing Subject");
			message.setText("Dear Mail Crawler," +
					"\n\n No spam to my email, please!");
 
			Transport.send(message);
 
			System.out.println("Done");
 
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}
Avatar_small
TNDGE Model Paper Cl 说:
2022年8月19日 21:56

Tamilnadu Board Model Paper 2023 Class 7 Pdf Download with Answers for Tamil Medium, English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams at official website. TNDGE Model Paper Class 7 New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.

Avatar_small
seo service UK 说:
2023年11月06日 01:21

Good to become visiting your weblog again, it has been months for me. Nicely this article that i've been waited for so long. I will need this post to total my assignment in the college, and it has exact same topic together with your write-up. Thanks, good share. This is a great article thanks for sharing this informative information.

Avatar_small
civaget 说:
2024年1月17日 02:50

오피타임's therapists pay attention to every detail during massages.

Avatar_small
civaget 说:
2024年1月17日 10:04

제주안마's ability to relieve stress, ease pain, and enhance mental focus makes it a top choice for those looking to unwind on Jeju Island.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter