Java code Implementation Send mail

Source: Internet
Author: User
Tags mailmessage

Package com.ust.email;

Import Java.io.File;
Import Java.util.Date;

Import Javax.activation.DataHandler;
Import Javax.activation.FileDataSource;
Import javax.mail.Address;
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 javax.mail.internet.MimeUtility;

/**

  • Send a message with an attachment
    */
    public class Attachmentmailsender {

    public static Boolean sendMail (Mailsenderinfo mailinfo) {
    //Determine if authentication is required
    Myauthenticator authenticator = null;< Br>if (Mailinfo.isvalidate ()) {
    //If authentication is required, create a password validator
    Authenticator = new Myauthenticator ( Mailinfo.getusername (),
    Mailinfo.getpassword ());
    }
    //The session
    Session sendmailsession = Session.getinstance (Mailinfo
    ) is constructed based on the properties and password validators sent by the message. GetProperties (), authenticator);
    try {
    //create a mail message based on session
    Message MailMessage = new MimeMessage (sendmailsession);
    Create a Mail sender address
    addresses from = new InternetAddress (mailinfo.getfromaddress ());
    Sets the sender of the mail message
    Mailmessage.setfrom (from);
    Create the recipient address of the message and set it to the mail message
    Address to = new InternetAddress (mailinfo.gettoaddress ());
    Mailmessage.setrecipient (message.recipienttype.to,to);
    //Set the subject of the mail message
    Mailmessage.setsubject (Mailinfo.getsubject ());
    Sets the time that a mail message is sent
    Mailmessage.setsentdate (new Date ());

        The Minimultipart class is a container class that contains objects of type MimeBodyPart Multipart Mainpart = new Mimemultipart ();    Create a mimebodypart bodypart html = new MimeBodyPart () that contains HTML content; Set HTML content html.setcontent (Mailinfo.getcontent (), "text/html;    CHARSET=GBK ");    Mainpart.addbodypart (HTML);    Add an attachment to a message string[] Attachfilenames = Mailinfo.getattachfilenames (); if (attachfilenames! = null && attachfilenames.length > 0) {//MimeBodyPart for storing mail attachments MimeBodyPart        attachment = NULL;        File file = null;            for (int i = 0; i < attachfilenames.length; i++) {attachment = new MimeBodyPart ();            Create a file data source according to the attachment files file = new file (Attachfilenames[i]);            Filedatasource FDS = new Filedatasource (file);            Attachment.setdatahandler (New DataHandler (FDS));            Set the file name for the attachment Attachment.setfilename (Mimeutility.encodeword (File.getname (), "GBK", null));    Mainpart.addbodypart (attachment);    }}//Set the Minimultipart object to the message content mailmessage.setcontent (Mainpart);    Send mail Transport.send (mailmessage); return true;}    catch (Exception e) {e.printstacktrace (); return false;}

    }

    public static void Main (string[] args) {
    Create message information
    Create message information
    Mailsenderinfo mailinfo = new Mailsenderinfo ();
    Mailinfo.setmailserverhost ("Mail. XXX.com ")//mail.qq.com this own look at the type of Baidu query, our company address does not disclose
    Mailinfo.setmailserverport ("25");
    Mailinfo.setvalidate (TRUE);
    Mailinfo.setusername ("User name");
    Mailinfo.setpassword ("password");
    Mailinfo.setfromaddress ("Your Mailbox");
    Mailinfo.settoaddress ("Mail Inbox");
    Mailinfo.setsubject ("MyMail test");
    Mailinfo.setcontent ("My mail test \n\rmy tests mail\n\r");
    Attachment
    string[] FileNames = new String[3];
    Filenames[0] = "C:/temp/new.txt";
    FILENAMES[1] = "C:/temp/test.wav";
    FILENAMES[2] = "c:/temp/mary_photo.jpg";
    Mailinfo.setattachfilenames (FileNames);

    AttachmentMailSender.sendMail(mailInfo);

    }
    }

Java code Implementation Send mail

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.