Java開発–JavaMailでメールを送信する

Javaコード
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMail {
private static String host = “smtp.xxx.com";
private static String user = “user_mail";
private static String pwd = “pwd_mail";

private static String from = “fromuser@sample1.com";
private static String to = “touser@sample2.com";
private static String subject = “from changfa tun “;

public void setAddress(String from, String to, String subject) {
this.from = from;
this.to = to;
this.subject = subject;
}
public void send(String txt) {
Properties props = new Properties();
props.put(“mail.smtp.host", host);
props.put(“mail.smtp.auth", “true");
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
message.setSubject(subject);
Multipart multipart = new MimeMultipart();

BodyPart contentPart = new MimeBodyPart();
contentPart.setText(txt);
multipart.addBodyPart(contentPart);
message.setContent(multipart);
message.saveChanges();
Transport transport = session.getTransport(“smtp");
transport.connect(host, user, pwd);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
SendMail kd = new SendMail();
kd.setAddress(from, to, subject);
kd.send(“this is send mail demo");
}
}

Java

Posted by arkgame