android realizes automatic mail sending
- 2021-09-16 08:13:53
- OfStack
This article example for everyone to share the implementation of an android e-mail automatic demo. Support 163, qq mailboxes
Need to add activation. jar, additionnal. jar and mail. jar
The first is an EmailSender class
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailSender {
private Properties properties;
private Session session;
private Message message;
private MimeMultipart multipart;
public EmailSender() {
super();
this.properties = new Properties();
}
public void setProperties(String host,String post){
// Address
this.properties.put("mail.smtp.host",host);
// Port number
this.properties.put("mail.smtp.post",post);
// Whether to verify
this.properties.put("mail.smtp.auth",true);
this.session=Session.getInstance(properties);
this.message = new MimeMessage(session);
this.multipart = new MimeMultipart("mixed");
}
/**
* Set Recipients
* @param receiver
* @throws MessagingException
*/
public void setReceiver(String[] receiver) throws MessagingException{
Address[] address = new InternetAddress[receiver.length];
for(int i=0;i<receiver.length;i++){
address[i] = new InternetAddress(receiver[i]);
}
this.message.setRecipients(Message.RecipientType.TO, address);
}
/**
* Set up Mail
* @param from Source
* @param title Title
* @param content Content
* @throws AddressException
* @throws MessagingException
*/
public void setMessage(String from,String title,String content) throws AddressException, MessagingException{
this.message.setFrom(new InternetAddress(from));
this.message.setSubject(title);
// Used in plain text setText() All right, but if you have attachments, you can't display the contents
MimeBodyPart textBody = new MimeBodyPart();
textBody.setContent(content,"text/html;charset=gbk");
this.multipart.addBodyPart(textBody);
}
/**
* Add attachments
* @param filePath File path
* @throws MessagingException
*/
public void addAttachment(String filePath) throws MessagingException{
FileDataSource fileDataSource = new FileDataSource(new File(filePath));
DataHandler dataHandler = new DataHandler(fileDataSource);
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDataHandler(dataHandler);
mimeBodyPart.setFileName(fileDataSource.getName());
this.multipart.addBodyPart(mimeBodyPart);
}
/**
* Send Mail
* @param host Address
* @param account Account name
* @param pwd Password
* @throws MessagingException
*/
public void sendEmail(String host,String account,String pwd) throws MessagingException{
// Sending time
this.message.setSentDate(new Date());
// Content, text and attachments sent
this.message.setContent(this.multipart);
this.message.saveChanges();
// Create a mail sending object and specify that it uses the SMTP Protocol to send mail
Transport transport=session.getTransport("smtp");
// Login mailbox
transport.connect(host,account,pwd);
// Send Mail
transport.sendMessage(message, message.getAllRecipients());
// Close the connection
transport.close();
}
}
The following is the mainactivity code
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btnOK;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOK = (Button) findViewById(R.id.button);
btnOK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// sendEmail();
// Time-consuming operations need to be threaded ... There are several novices who have this problem
new Thread(new Runnable() {
@Override
public void run() {
try {
EmailSender sender = new EmailSender();
// Set the server address and port, and search it on the Internet
sender.setProperties("smtp.163.com", "25");
// Set the sender, message title, and text content separately
sender.setMessage(" Yours 163 Email account number ", "EmailSender", "Java Mail ! ");
// Set Recipients
sender.setReceiver(new String[]{" Recipient mailbox "});
// Add attachments
// The path of this attachment is in my mobile phone. If you want to send it, you have to change it to the correct path in your mobile phone
// sender.addAttachment("/sdcard/DCIM/Camera/asd.jpg");
// Send Mail
sender.sendEmail("smtp.163.com", " Yours 163 Email account number ", " Your email password ");//<span style="font-family: Arial, Helvetica, sans-serif;">sender.setMessage(" Yours 163 Email account number ", "EmailS//ender", "Java Mail ! "); There are two email accounts to 1 To </span>
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
});
}
}