Send mail using JavaMail

Source: Internet
Author: User

The code is as follows:

Package demo;





import java.util.Properties;





import Javax.activation.DataHandler;


import Javax.activation.DataSource;


import Javax.activation.FileDataSource;


import Javax.mail.BodyPart;


import Javax.mail.Message;


import Javax.mail.Multipart;


import javax.mail.Session;


import Javax.mail.Transport;


import javax.mail.internet.InternetAddress;


import Javax.mail.internet.MimeBodyPart;


import Javax.mail.internet.MimeMessage;


import Javax.mail.internet.MimeMultipart;





import Sun.misc.BASE64Encoder;





public class MailSender ... {





public static void Main (string[] args) ... {





MailSender sender = new MailSender ();


Sender.sethost ("smtp.sina.com.cn");


sender.setfromaddr ("");


sender.settoaddr ("");


sender.setusername ("wwwwwwww");


Sender.setpassword ("ssssssss");


Sender.settitle ("a few ideas on improving efficiency");


Sender.setattachpath ("C:singleuser.sql");


sender.setattachname ("Singleuser.sql");





Try ... {


Sender.sendmail ();


} catch (Exception e) ... {


E.printstacktrace ();


    }


  }





public void SendMail () throws Exception ... {





Properties Props = new properties ();


props.put ("Mail.smtp.host", GetHost ());


props.put ("Mail.smtp.auth", "true");


Session session = Session.getdefaultinstance (props);


Session.setdebug (TRUE);


MimeMessage message = new MimeMessage (session);


Message.setfrom (New InternetAddress (GETFROMADDR ()));


message.addrecipient (message.recipienttype.to,new internetaddress (Gettoaddr ()));


Message.setsubject (GetTitle ());


Multipart Multipart = new Mimemultipart ();


bodypart Contentpart = new MimeBodyPart ();


Contentpart.settext ("The details of the message is Here");


multipart. Addbodypart (Contentpart);


if (Getattachpath ()!= null && getattachname ()!= null) ... {


bodypart attachmentpart= New MimeBodyPart ();


DataSource Source = new Filedatasource (Getattachpath ());


Attachmentpart.setdatahandler (new DataHandler (source));


Base64encoder enc = new Base64encoder ();


attachmentpart.setfilename ("=? GBK? B? "+enc.encode (Getattachname (). GetBytes ()) +"? = ");


Multipart.addbodypart (Attachmentpart);


    }


message.setcontent (multipart);


message.savechanges ();


Transport transport = Session.gettransport ("SMTP");


Transport.connect (Host, GetUserName (), GetPassword ());


transport.sendmessage (Message, message.getallrecipients ());


Transport.close ();


  }





private String host = null;


private String fromaddr = null;


private String toaddr = null;


private String username = null;


private String password = null;


private String title = null;


private String attachpath = null;


private String attachname = null;





public String gethost () ... {


return host;


  }





public void Sethost (String host) ... {


this.host = host;


  }





public String getfromaddr () ... {


return fromaddr;


  }





public void Setfromaddr (String fromaddr) ... {


this.fromaddr = fromaddr;


  }





public String gettoaddr () ... {


return toaddr;


  }





public void Settoaddr (String toaddr) ... {


this.toaddr = toaddr;


  }





public String getusername () ... {


return username;


  }





public void Setusername (String username) ... {


this.username = Username;


  }





public String GetPassword () ... {


return password;


  }





public void SetPassword (String password) ... {


This.password = password;


  }





public String getTitle () ... {


return title;


  }





public void Settitle (String title) ... {


this.title = title;


  }





public String Getattachpath () ... {


return attachpath;


  }





public void Setattachpath (String attachpath) ... {


This.attachpath = Attachpath;


  }





public String getattachname () ... {


return attachname;


  }





public void Setattachname (String attachname) ... {


this.attachname = attachname;


  }


}

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.