⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendmailutil.java

📁 网上商城代码
💻 JAVA
字号:
package com.fendou.util.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailUtil {

	private MimeMessage message;
	private Properties props;
	private Session session;
	private String name;
	private String password;
	
	public SendMailUtil(String host,String name,String password){
		this.name=name;
		this.password=password;
		this.props=System.getProperties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");
		FendouAuthenticator auth=new FendouAuthenticator(name,password);
		session=Session.getDefaultInstance(props, auth);
		message=new MimeMessage(session);
	}
	
	public void setForm(String form){
		try {
			message.setFrom(new InternetAddress(form));
		} catch (AddressException e) {			
			e.printStackTrace();
		} catch (MessagingException e) {			
			e.printStackTrace();
		}
	}
	
	public void setTo(String to){
		try {
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		} catch (AddressException e) {			
			e.printStackTrace();
		} catch (MessagingException e) {			
			e.printStackTrace();
		}
	}
	
	public void setSubject(String subject){
		try {
			message.setSubject(subject);
		} catch (MessagingException e) {			
			e.printStackTrace();
		}
	}
	
	public void setText(String text){
		try {
			message.setText(text);
		} catch (MessagingException e) {			
			e.printStackTrace();
		}
	}
	
	public boolean send(){
		try {
			Transport port =session.getTransport("smtp");
			port.connect((String)props.getProperty("mail.smtp.host"), name, password);
			port.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
			port.close();
			return true;
		} catch (NoSuchProviderException e) {			
			e.printStackTrace();
			return false;
		}catch (MessagingException e) {			
			e.printStackTrace();
			return false;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -