How Java code sends a message with a picture

Source: Internet
Author: User

This chapter records three, 1.java send normal mail 2.java send messages with attachments 3.java send a message with pictures

1.java Send ordinary mail--the first to set the following specific parameters:

public static String username = "***@163.com"; Service mailbox (from mailbox)
public static String Password = "* * * *"; Email password
public static String Sendernick = "* * *"; Sender Nickname
Private Properties props; System Properties----The specific configuration is given below.
Private session session; Mail Session Object
Private MimeMessage mimemsg; MIME Message Object
Private Mimemultipart MP; Multipart object, message content, title, attachments, pictures and other content are added to the MimeMessage object

2. Now look at how the properties of the System Properties property should be configured:
 props = System.getproperties (); 
Props.put ("Mail.smtp.auth", "true"),//mail.smtp.auth property, or a user name and password that provides SMTP transport and connection to the SMTP server-True if user authentication is required
Props.put ("Mail.transport.protocol", "SMTP");//Protocol
Props.put ("Mail.smtp.host", "* * * * * * * * * * *");//server transmitting the Mail protocol
Props.put ("Mail.smtp.port", "25");//The port number of the mail server
Props.put ("username", username);//Email address
Props.put ("Password ", password);//Issue the Mailbox password
props.put (" Mail.smtp.starttls.enable "," true ");
Props.put ("Mail.smtp.ssl.trust", "* * * * *");

3. Next is configure Seesion

Myauthenticator Myauth = new Myauthenticator (sendernick, password);
session = Session.getdefaultinstance (props, myauth);

4. Method for user authentication:---
class Myauthenticator extends Authenticator {
String userName = "";
String password = "";

Public Myauthenticator () {
}

Public Myauthenticator (string userName, string password) {
this.username = userName;
this.password = password;
}

protected Passwordauthentication getpasswordauthentication () {
return new passwordauthentication (userName, password);
}
}
Increased the user's authentication in 3,4, can avoid sending mail to the mailbox, reported error:535 5.7.3 Authentication unsuccessful problem.

5. Next, fill in the message body and picture:

mimemsg = new MimeMessage (session);
MP = new Mimemultipart ("related");

Custom Sender Nickname
String Nick = "";
try {
Nick = Javax.mail.internet.MimeUtility.encodeText (Sendernick);
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}
Set Sender
Mimemsg.setfrom (New InternetAddress (from, Nick)),--from Sender's address, nickname
Set recipient
if (to = null && to.length () > 0) {
Mimemsg.setrecipients (Message.RecipientType.TO, Internetaddress.parse (to));
}
Set CC people
if (cc! = null && cc.length () > 0) {
Mimemsg.setrecipients (Message.RecipientType.CC, Internetaddress.parse (CC));
}
Set theme
Mimemsg.setsubject (subject);

Set up a picture
MimeBodyPart Text = new MimeBodyPart ();
Text.setcontent ("This message is automatically sent to the system MimeBodyPart img = new MimeBodyPart ();
DataHandler dh = new DataHandler (New Filedatasource ("d://chrysanthemum.jpg"));//Picture path
Img.setdatahandler (DH);
Img.setcontentid ("a");

MimeBodyPart img2 = new MimeBodyPart ();
DataHandler DH2 = new DataHandler (New Filedatasource ("d://chrysanthemum.jpg"));//Second picture path
Img2.setdatahandler (DH2);
Img2.setcontentid ("B");

Mimemultipart mm = new Mimemultipart ();
Mm.addbodypart (text);
Mm.addbodypart (IMG);
Mm.setsubtype ("related");//Set the relationship between the body and the picture
The body of the drawing class and the text
MimeBodyPart all = new MimeBodyPart ();
All.setcontent (mm);
Mimemultipart mm2 = new Mimemultipart ();
Mm2.addbodypart (All);
Mm2.addbodypart (IMG2);
Mm2.setsubtype ("mixed");



To set up attachments:
                if (errorlist! = null && errorlist.size () > 0) {
for (String file:errorlist) {
Contentpart = new MimeBodyPart ();
Filedatasource Source = new Filedatasource (file);
Contentpart.setdatahandler (new DataHandler (source));
string[] imgname = File.replace (File.separator, ","). Split (",");
int num = imgname.length-1;
Contentpart.setfilename (Imgname[num]);
Multipart.addbodypart (Contentpart);
}
}
                Mimemsg.setcontent (mm2);
Mimemsg.savechanges ();
Send mail
if (Props.get ("Mail.smtp.auth"). Equals ("true")) {
Transport Transport = Session.gettransport ("SMTP");
Transport.connect (String) props.get ("Mail.smtp.host"), (String) props.get ("username"), (String) Props.get (" Password "));
Transport.sendmessage (Mimemsg, mimemsg.getallrecipients ());
Transport.close ();
} else {
Transport.send (MIMEMSG);
}
System.out.println ("Mail sent successfully");



How Java code sends a message with a picture

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.