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

📄 email.java

📁 用Java编写的一个可下载MP3
💻 JAVA
字号:
package org.serain.shmily.mail;

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;

public class Email {
	private String to="",from="",cc="",bcc="",mailhost="",subject="",text="",attachment="",username="",password="",proxyHost=null,proxyPort=null;
	private MimeMessage mimeMessage; 
	private Session session;
	
	//有验证有附件
	public Email(String to,String from,String cc,String bcc,String mailhost,String subject,String text,String attachment,String username,String password){
		this.to=to;
		this.from=from;
		this.cc=cc;
		this.bcc=bcc;
		this.mailhost=mailhost;
		this.subject=subject;
		this.text=text;
		this.attachment=attachment;
		this.username=username;
		this.password=password;
	}
	//有验证有附件有代理
	public Email(String to,String from,String cc,String bcc,String mailhost,String subject,String text,String attachment,String username,String password,String proxyHost,String proxyPort){
		this.to=to;
		this.from=from;
		this.cc=cc;
		this.bcc=bcc;
		this.mailhost=mailhost;
		this.subject=subject;
		this.text=text;
		this.attachment=attachment;
		this.username=username;
		this.password=password;
		this.proxyHost=proxyHost;
		this.proxyPort=proxyPort;
	}

	public void send(){
		try{
            //获得系统属性
			Properties pro=System.getProperties();
			
			//设置smtp服务器
			if(mailhost.trim().equals("")){
				throw new Exception("mailhost为空");
			}else{
				pro.put("mail.smtp.host", mailhost);
			}
			
			
			//设置代理
			if(proxyHost!=null&&proxyPort!=null){
				pro.put("http.proxySet", true);
				pro.put("http.proxyHost", proxyHost);
				pro.put("http.proxyPort", proxyPort);
			}
			
			//设置验证获得邮件会话对象
			if(username.trim().equals("")||password.trim().equals("")){
				throw new Exception("用户名或者密码为空");
			}else{
				pro.put("mail.smtp.auth", "true");
				session=Session.getDefaultInstance(pro,new AutherBean(username, password ));
				//session=Session.getDefaultInstance(pro,null);
			}
			
			//创建mime邮件对象
			mimeMessage=new MimeMessage(session);
			
			//设置收件人,抄送人,暗送人
			if(to.trim().equals("")){
				throw new Exception("收件人为空");
			}else{
				mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
			}
			
			if(!cc.equals(""))//可以为空,空时不要
			mimeMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
			if(!bcc.equals(""))//可以为空,空时不要
			mimeMessage.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
			
			//设置发件人
			if(from.trim().equals("")){
				throw new Exception("发件人为空");
			}else{
				mimeMessage.setFrom(new InternetAddress(from));
			}
			
			
			//设置主题
			if(subject.trim().equals("")){
				throw new Exception("主题为空");
			}else{
				mimeMessage.setSubject(subject,"GBK");
			}
			
			
			//设置邮件内容,附件
			if(!attachment.equals("")&&!text.equals("")){
				MimeBodyPart textPart=new MimeBodyPart();
				textPart.setText(text,"GBK");
				MimeBodyPart attachmentPart=new MimeBodyPart();
				FileDataSource fds=new FileDataSource(attachment);
				attachmentPart.setDataHandler(new DataHandler(fds));
				attachmentPart.setFileName(fds.getName());
				Multipart mp=new MimeMultipart();
				mp.addBodyPart(textPart);
				mp.addBodyPart(attachmentPart);
				mimeMessage.setContent(mp);
			}else if(!text.equals("")){
				mimeMessage.setText(text);
			}
			
			//设置日期
			mimeMessage.setSentDate(new Date());
			
			//发送邮件
			mimeMessage.saveChanges();
			Transport transport=session.getTransport("smtp");
			transport.connect(mailhost, username, password);
			transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
            //关闭
			System.out.println("ok");
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

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