1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| package util.utilmail;
import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Properties;
public class UtilMail { private static Properties prop = null; private static Session session =null; private static MimeMessage mimeMessage =null;
private static String sendAddress = null; private static String receiveAddress = null; private static String sendAddressPassword = null; private static String contentType = null; private static String subjectType = null; private static String sendNicknameType = null; private static String sendNickname = null;
static { Properties properties = new Properties(); prop = new Properties(); try { InputStreamReader inputStreamReader = new InputStreamReader(UtilMail.class.getClassLoader().getResourceAsStream("mail_user.properties"),"GBK"); InputStreamReader inputStreamReader1 = new InputStreamReader(UtilMail.class.getClassLoader().getResourceAsStream("mail_connection.properties"),"GBK"); prop.load(inputStreamReader1); properties.load(inputStreamReader); } catch (IOException e) { e.printStackTrace(); }
sendAddress = properties.getProperty("sendAddress"); receiveAddress = properties.getProperty("receiveAddress"); sendAddressPassword = properties.getProperty("sendAddressPassword"); contentType = properties.getProperty("contentType"); subjectType = properties.getProperty("subjectType"); sendNicknameType = properties.getProperty("sendNicknameType"); sendNickname = properties.getProperty("sendNickname"); session = Session.getInstance(prop); mimeMessage = new MimeMessage(session); }
public static void sendMessage(String nickname,String subject,String content,String receiver) throws MessagingException, UnsupportedEncodingException { mimeMessage.setFrom(new InternetAddress(sendAddress,nickname,sendNicknameType)); mimeMessage.setContent(content,contentType); mimeMessage.setSubject(subject,subjectType); mimeMessage.setRecipients(MimeMessage.RecipientType.TO,receiver); mimeMessage.setSentDate(new Date()); Transport.send(mimeMessage,sendAddress,sendAddressPassword); }
public static void sendMessage(String subject,String content) throws MessagingException, UnsupportedEncodingException { mimeMessage.setFrom(new InternetAddress(sendAddress,sendNickname,sendNicknameType)); mimeMessage.setContent(content,contentType); mimeMessage.setSubject(subject,subjectType); mimeMessage.setRecipients(MimeMessage.RecipientType.TO,receiveAddress); mimeMessage.setSentDate(new Date()); Transport.send(mimeMessage,sendAddress,sendAddressPassword); } }
|