Use javamail in android to send email attachments

Source: Internet
Author: User

[Java] to use javamail, you must first download three JAR packages and import the project activation. jar additonnal. jar mail. jar.
 
Import method: project-> properties-> java build path-> libraries-> add external jars
 
Then, add the network access permission to the android project.
 
<Uses-permission android: name = "android. permission. INTERNET"> </uses-permission>
 
Finally, load the following package in the program
 
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
 
 
Import java. util. Properties;
Import javax. activation. DataHandler;
Import javax. activation. FileDataSource;
Import javax. mail .*;
Import javax. mail. internet .*;
Import javax. mail. PasswordAuthentication;
 
 
The code for calling a function is as follows:
 
 
 
Class MyAuthenticator
Extends javax. mail. Authenticator {
Private String strUser;
Private String strPwd;
Public MyAuthenticator (String user, String password)
{
This. strUser = user;
This. strPwd = password;
}
 
 
Protected PasswordAuthentication getPasswordAuthentication ()
{
Return new PasswordAuthentication (strUser, strPwd );
}
}
 
 
 
Public void send_mail_file (String str_to_mail, String str_from_mail, String str_smtp, String str_user, String str_pass, String str_title, String str_body, String str_file_path)
{
Log. v ("lengfeng", "send_mail_file ");
 
 
String host = str_smtp; // the email address server on which the sender sends an email.
String from = str_from_mail; // the sender's mailbox)
String to = str_to_mail; // mail destination (recipient's mailbox)
 
 
Log. v ("lengfeng", str_smtp );
Log. v ("lengfeng", str_from_mail );
Log. v ("lengfeng", str_to_mail );

Properties props = System. getProperties (); // Get system properties
 
 

Props. put ("mail. smtp. host", host); // Setup mail server
 
 

Props. put ("mail. smtp. auth", "true "); //
 
 
MyAuthenticator myauth = new MyAuthenticator (str_user, str_pass); // Get session

Session session = Session. getDefaultInstance (props, myauth );
 
 

MimeMessage message = new MimeMessage (session); // Define message

 
 

Try {
Message. setFrom (new InternetAddress (from); // Set the from address
} Catch (AddressException e ){
E. printStackTrace ();
} Catch (MessagingException e ){
E. printStackTrace ();
}
 
 

Try {
Message. addRecipient (Message. RecipientType. TO, new InternetAddress (to); // Set the to address
} Catch (AddressException e ){
E. printStackTrace ();
} Catch (MessagingException e ){
E. printStackTrace ();
}
 
 

Try {
Message. setSubject (str_title); // Set the subject
} Catch (MessagingException e ){
E. printStackTrace ();
}
 
 

Try {
Message. setText (str_body); // Set the content
} Catch (MessagingException e ){
E. printStackTrace ();
}
 
 

MimeBodyPart attachPart = new MimeBodyPart ();
FileDataSource fds = new FileDataSource (str_file_path); // open the file to be sent
Try {
AttachPart. setDataHandler (new DataHandler (fds ));
} Catch (MessagingException e ){
E. printStackTrace ();
}
Try {
AttachPart. setFileName (fds. getName ());
} Catch (MessagingException e ){
E. printStackTrace ();
}


MimeMultipart allMultipart = new MimeMultipart ("mixed"); // attachment
Try {
AllMultipart. addBodyPart (attachPart); // Add
} Catch (MessagingException e ){
E. printStackTrace ();
}
Try {
Message. setContent (allMultipart );
} Catch (MessagingException e ){
E. printStackTrace ();
}
Try {
Message. saveChanges ();
} Catch (MessagingException e ){
E. printStackTrace ();
}
 
 
Try {
Transport. send (message); // start sending
} Catch (MessagingException e ){
E. printStackTrace ();
}

}
To use javamail, you must first download three JAR packages and import the project activation. jar additonnal. jar mail. jar.

Import method: project-> properties-> java build path-> libraries-> add external jars

Then, add the network access permission www.2cto.com to the android project.

<Uses-permission android: name = "android. permission. INTERNET"> </uses-permission>

Finally, load the following package in the program


Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;


Import java. util. Properties;
Import javax. activation. DataHandler;
Import javax. activation. FileDataSource;
Import javax. mail .*;
Import javax. mail. internet .*;
Import javax. mail. PasswordAuthentication;


The code for calling a function is as follows:

 

Class MyAuthenticator
Extends javax. mail. Authenticator {
Private String strUser;
Private String strPwd;
Public MyAuthenticator (String user, String password)
{
This. strUser = user;
This. strPwd = password;
}


Protected PasswordAuthentication getPasswordAuthentication ()
{
Return new PasswordAuthentication (strUser, strPwd );
}
}

 

Public void send_mail_file (String str_to_mail, String str_from_mail, String str_smtp, String str_user, String str_pass, String str_title, String str_body, String str_file_path)
{
Log. v ("lengfeng", "send_mail_file ");


String host = str_smtp; // the email address server on which the sender sends an email.
String from = str_from_mail; // the sender's mailbox)
String to = str_to_mail; // mail destination (recipient's mailbox)


Log. v ("lengfeng", str_smtp );
Log. v ("lengfeng", str_from_mail );
Log. v ("lengfeng", str_to_mail );

Properties props = System. getProperties (); // Get system properties



Props. put ("mail. smtp. host", host); // Setup mail server



Props. put ("mail. smtp. auth", "true "); //


MyAuthenticator myauth = new MyAuthenticator (str_user, str_pass); // Get session

Session session = Session. getDefaultInstance (props, myauth );



MimeMessage message = new MimeMessage (session); // Define message



Try {
Message. setFrom (new InternetAddress (from); // Set the from address
} Catch (AddressException e ){
E. printStackTrace ();
} Catch (MessagingException e ){
E. printStackTrace ();
}



Try {
Message. addRecipient (Message. RecipientType. TO, new InternetAddress (to); // Set the to address
} Catch (AddressException e ){
E. printStackTrace ();
} Catch (MessagingException e ){
E. printStackTrace ();
}



Try {
Message. setSubject (str_title); // Set the subject
} Catch (MessagingException e ){
E. printStackTrace ();
}



Try {
Message. setText (str_body); // Set the content
} Catch (MessagingException e ){
E. printStackTrace ();
}



MimeBodyPart attachPart = new MimeBodyPart ();
FileDataSource fds = new FileDataSource (str_file_path); // open the file to be sent
Try {
AttachPart. setDataHandler (new DataHandler (fds ));
} Catch (MessagingException e ){
E. printStackTrace ();
}
Try {
AttachPart. setFileName (fds. getName ());
} Catch (MessagingException e ){
E. printStackTrace ();
}


MimeMultipart allMultipart = new MimeMultipart ("mixed"); // attachment
Try {
AllMultipart. addBodyPart (attachPart); // Add
} Catch (MessagingException e ){
E. printStackTrace ();
}
Try {
Message. setContent (allMultipart );
} Catch (MessagingException e ){
E. printStackTrace ();
}
Try {
Message. saveChanges ();
} Catch (MessagingException e ){
E. printStackTrace ();
}


Try {
Transport. send (message); // start sending
} Catch (MessagingException e ){
E. printStackTrace ();
}

}

 


From sunset hut

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.