[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