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

📄 smtpmail.java

📁 使用java的GUI方式实现了群发邮件的功能
💻 JAVA
字号:
package com.v512;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;

import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.mail.smtp.SMTPTransport;

public class SmtpMail { 

	public String send(String host, String from, String to, String subject, String content, String file) {
		SMTPTransport t=null;
		boolean auth=false;
		Properties props = System.getProperties();
		props.put("mail.smtp.host", host);
		//props.put("mail.smtp.auth", "true");
	//	  Provider p = new Provider(Provider.Type.TRANSPORT, "smtp",	"smtpsend$SMTPExtension", "JavaMail demo", "no version");
	//			    props.put("mail.smtp.class", "smtpsend$SMTPExtension");
	
		try {
		Session session = Session.getInstance(props, null);
	//	session.addProvider(p);
		// construct the message
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(from));
		msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
		msg.setSubject(subject);
		
		if (file != null) {
			// Attach the specified file.
			// We need a multipart message to hold the attachment.
			MimeBodyPart mbp1 = new MimeBodyPart();
			mbp1.setText(content);
			MimeBodyPart mbp2 = new MimeBodyPart();
			mbp2.attachFile(file);
			MimeMultipart mp = new MimeMultipart();
			mp.addBodyPart(mbp1);
			mp.addBodyPart(mbp2);
			msg.setContent(mp);
		} else {
			// If the desired charset is known, you can use		
			// setText(text, charset)			
			msg.setText(content);
		}
		msg.setHeader("V512-MaileServer", "www.v512.com");
		msg.setSentDate(new Date());

	//	Transport.send(msg);
		
		t = (SMTPTransport) session.getTransport("smtp");
		
			if (auth)
				t.connect(host, "liuwei", "winamp");
			else{
			t.connect();
			}
			t.sendMessage(msg, msg.getAllRecipients());
			t.close();
		}catch(Exception e){			
			e.printStackTrace();
		}		
		finally {			
			try {
				t.close();
			} catch (MessagingException e) {				
				e.printStackTrace();
			}
		}
		return  t.getLastServerResponse();
	}
}

⌨️ 快捷键说明

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