Javaweb Basics-Mail Sending

Source: Internet
Author: User
Tags imap

Javamail-java provides a set of APIs to send and receive mail

There is a mail server in the middle:
The client is sent to the mail server, and another receiving client is downloaded from the mail server
(The most stable or 163,qq a bit not reliable)

SMTP protocol port 25
The full name of SMTP is "Simple Mail Transfer Protocol", which is simply the message Transfer Protocol.
It is a set of specifications for transferring messages from the source address to the destination, which controls how messages are relayed.
The SMTP protocol is a TCP/IP protocol cluster that helps each computer find its next destination when sending or relaying letters.
The SMTP server is the outgoing mail server that follows the SMTP protocol. SMTP authentication, which simply requires that the account name must be provided in the
and password before you can log in to the SMTP server, which makes it possible for spammers to be free. Increased SMTP authentication.
The goal is to prevent users from being harassed by spam.
SMTP[2] has been the de facto standard for e-mail transmission.

Pop protocol POP3 Post Office Protocol third edition port 110
The Pop Post Office Protocol is responsible for retrieving e-mail from the mail server.

IMAP protocol
Internet Information Access Protocol (IMAP) is a new protocol that is better than pop.

(understanding) Teinet e-mail (BASE64 Sun package, Java javax underlying dependencies, not recommended, reported warning)
Use prohibited classes slightly, Baidu.

Send using JavaMail: The first step is the guide pack, which requires two packages: Mail.jar_activation.jar
Core class
Session: Similar to connection, but httpsession steps different from the Web
MimeMessage: Mail Object
TransPort: Used to send mail
Hair strap attachment with no attachment (multi-part body) Mimemultipart MimeBodyPart

Mailutils Tool Class Itcast-tool

Give a small example:

This is the jar package.

  

 PackageCn.itcaat.mail;ImportJava.io.File;Importjava.io.IOException;Importjava.util.Properties;ImportJavax.mail.Authenticator;ImportJavax.mail.Message.RecipientType;Importjavax.mail.MessagingException;Importjavax.mail.PasswordAuthentication;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.AddressException;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeMessage;Importorg.junit.Test;ImportCn.itcast.mail.AttachBean;ImportCn.itcast.mail.Mail;Importcn.itcast.mail.MailUtils;/*** Test e-mail demo without attachments *@authorjiangbei01 **/ Public classDemo01 {@Test Public voidFUN1 ()throwsaddressexception, messagingexception{/** 1. Get session*/Properties Props=NewProperties (); //Host nameProps.setproperty ("Mail.host", "smtp.163.com"); //whether certification is requiredProps.setproperty ("Mail.smtp.auth", "true"); Authenticator Auth=NewAuthenticator () {@Overrideprotectedpasswordauthentication getpasswordauthentication () {//The return value type has a generic constructor                return NewPasswordauthentication ("Ttbeifen1", "tt2924");        }        }; Session Session=session.getinstance (props, auth); //Get Mail ObjectMimeMessage msg =NewMimeMessage (session); //cc Dark send here slightlyMsg.setfrom (NewInternetAddress ("[email protected]");//SenderMsg.setrecipients (recipienttype.to, "[email protected]");//RecipientMsg.setsubject ("This is a test from DAY22"); Msg.setcontent ("Fried chicken and Beer", "Text/html;charset=utf-8"); //Sendtransport.send (msg); }    /*** Use the Gadget class, guide the package Itcast-tool *@throwsIOException *@throwsmessagingexception*/@Test Public voidFun2 ()throwsmessagingexception, ioexception{//Get SessionSession session = Mailutils.createsession ("smtp.163.com", "Ttbeifen1", "tt2924"); //get the Message object parameters in turn sender, recipient, subject, bodyMail mail =NewMail ("[Email protected]", "[email protected]", "This is the subject", "This is the body"); //add attachments to mailAttachbean Attachbean =NewAttachbean (NewFile ("F:/2.jpg"), "Butterfly Valley");        Mail.addattach (Attachbean); //SendMailutils.send (session, mail); }}

Javaweb Basics-Mail Send

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.