📄 sendmailaddfile.txt
字号:
Jmail带附件的邮件发送:
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendMailAddFile {
public SendMailAddFile() {
}
public static void main(String[] args) throws Exception {
SendMailAddFile send = new SendMailAddFile();
send.sendFile();
}
public void sendFile() throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.smtp.host","smtp.sina.com");
prop.setProperty("mail.smtp.auth","true");
MyAuthenticator authenticator = new MyAuthenticator();
Session session = Session.getInstance(prop,authenticator);
MimeMessage message = new MimeMessage(session);
Address from = new InternetAddress("wpabbs@sina.com"); //你的邮箱
Address to = new InternetAddress("要发送的Email"); //她的邮箱
message.setFrom(from);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject("o(∩_∩)o...哈哈");
Multipart mpart = new MimeMultipart();
MimeBodyPart body = new MimeBodyPart();
body.setText("测试带附件的邮件发送情况");
mpart.addBodyPart(body);
body = new MimeBodyPart();
DataSource ds = new FileDataSource("c://1.jpg");
DataHandler dh = new DataHandler(ds);
body.setDataHandler(dh);
body.setFileName("readme.jpg");
mpart.addBodyPart(body);
message.setContent(mpart);
Transport trans = session.getTransport("smtp");
trans.connect("smtp.sina.com","用户名","密码"); //你的用户名及密码
trans.send(message,message.getAllRecipients());
trans.close();
System.out.println("send ok!");
}
public class MyAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("用户名","密码"); //你的用户名及密码
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -