After the modification can send the attachment, CC, JavaBean, vomiting blood recommended ~ (javamail example)

Source: Internet
Author: User
Tags mail
/*
*author:tyfun
*datetime:2003.01.09
*package:com.westarsoft.function
*/

Package com.westarsoft.function;

Import java.util.*;
Import java.io.*;
Import javax.mail.*;
Import javax.mail.internet.*;
Import javax.activation.*;

public class SendMail {
private String smtpserver = new string ();
private string from = new String ();
private String subject = new String ();
private string content = new string ();
private String user = new String ();
private String Password = new string ();
private String fileattachment = new string ();
Public String Getsmtpserver () {
return smtpserver;
}
public void Setsmtpserver (String smtpserver) {
This. SmtpServer = SmtpServer;
}
Public String Getfrom () {
return from;
}
public void Setfrom (String from) {
This.from = from;
}
Public String Getsubject () {
return subject;
}
public void Setsubject (String subject) {
This.subject = subject;
}
Public String getcontent () {
return content;
}
public void SetContent (String content) {
this.content = content;
}
Public String GetUser () {
return user;
}
public void SetUser (String user) {
This.user = user;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
Public String getfileattachment () {
return fileattachment;
}
public void Setfileattachment (String fileattachment) {
This.fileattachment = fileattachment;
}

public void Sendmailto (String to,string cc,string bcc) {
try {
Properties Props = new properties ();
Session sendmailsession;
Store store;
Transport transport;

Sendmailsession = session.getinstance (props, null);
Props.put ("Mail.smtp.host", smtpserver);
MimeMessage newmessage = new MimeMessage (sendmailsession);
Newmessage.setfrom (New InternetAddress (from));
Newmessage.setsubject (subject);
Newmessage.setsentdate (New Date ());
/*
StringTokenizer Tokento = new StringTokenizer (To, ",");
internetaddress[] Addrarrto = new Internetaddress[tokento.counttokens ()];
int i = 0;
while (Tokento.hasmoretokens ()) {
Addrarrto[i] = new InternetAddress (Tokento.nexttoken (). toString ());
i++;
}
*/
Transport = Sendmailsession.gettransport ("SMTP");
Transport.connect (smtpserver, user, password);
Newmessage.setrecipients (Message.RecipientType.TO, Addrarrto);
Newmessage.setrecipients (Message.RecipientType.TO, Internetaddress.parse (to));
Newmessage.setrecipients (Message.RecipientType.CC, Internetaddress.parse (CC));
Newmessage.setrecipients (Message.RecipientType.BCC, Internetaddress.parse (BCC));

MimeBodyPart Messagebodypart = new MimeBodyPart ();
Messagebodypart.settext (content);
Multipart Multipart = new Mimemultipart ();
Multipart.addbodypart (Messagebodypart);
Messagebodypart = new MimeBodyPart ();
DataSource Source = new Filedatasource (fileattachment);
Messagebodypart.setdatahandler (new DataHandler (source));
Messagebodypart.setfilename (fileattachment);
Multipart.addbodypart (Messagebodypart);
Newmessage.setcontent (multipart);

Transport.send (Newmessage);
}
catch (Exception e) {
System.out.println (e);
}
}
}





<%
if (Request.getmethod (). Equals ("POST")) {
SendMail mail = new SendMail ();
Mail.setsmtpserver ("200.1.1.157");
Mail.setuser ("lint");
Mail.setpassword ("30320");
Mail.setfrom (Request.getparameter ("from"));
Mail.setsubject (Request.getparameter ("subject"));
Mail.setcontent (Request.getparameter ("content"));
Mail.setfileattachment (Request.getparameter ("filename"));
Mail.sendmailto (Request.getparameter ("to"), Request.getparameter ("CC"), Request.getparameter ("Bcc"));
}
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.