JAVA MAIL API ISLEMLERI
Java ile mail gönderme - Mail Subjectlerini okuma
Mail server host'u mail adresi ve şifre bilgilerinizi ilgili alanlara girmeniz yeterli. Kod ve jarlar için maven projesinin pom.xml'ine ekleyeceğiniz dependency'ler aşağıdaki gibi.
İyi çalışmalar.
>
>
>log4j >
>log4j >
>1.2.17 >
>jar >
>
>
>javax >
>javaee-web-api >
>6.0 >
>provided >
>
>
>javax.mail >
>mail >
>1.4.7 >
>jar >
>
>
>org.apache.commons >
>commons-collections4 >
>4.0 >
>
>
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.FetchProfile;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.UIDFolder.FetchProfileItem;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.search.FlagTerm;
import org.apache.commons.lang3.StringUtils;
/**
*
* @author BeytullahC
*
* Simple demonstration of using the javax.mail API.
*
* Run from the command line. Please edit the implementation to use correct
* email addresses and host name.
*/
public final class MailSender {
public static void main(String... aArguments) {
try {
sendMail("senderMailAddress", "password", "smtpHost", new String[]{"toMail1", "toMail2"});
} catch (Exception ex) {
Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static ArrayList readMailsImap(String host, String mail, String password) throws Exception {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.imaps.ssl.trust", host);
props.setProperty("mail.imaps.connectionpoolsize", "10");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect(host, mail, password);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
FetchProfile profile = new FetchProfile();
profile.add(FetchProfileItem.CONTENT_INFO);
profile.add("X-mailer");
Message msg[] = inbox.search(new FlagTerm(
new Flags(Flag.SEEN), false));
ArrayList list = new ArrayList<>();
SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss dd/MM/yy", Locale.ENGLISH);
inbox.fetch(msg, profile);
for (Message message : msg) {
Multipart PART = new MimeMultipart();
MessageDetail detail = new MessageDetail(StringUtils.join(message.getFrom(), ", "), message.getSubject(), formatter2.parse(formatter2.format(message.getSentDate())));
list.add(detail);
}
return list;
} catch (MessagingException mex) {
throw new Exception(mex.getMessage());
}
}
public static void sendMail(final String username, final String password, final String host, final String[] toMail) {
// Recipient's email ID needs to be mentioned.
String to = StringUtils.join(toMail, ",");
// Sender's email ID needs to be mentioned
String from = username;//change accordingly
// Assuming you are sending email through relay.jangosmtp.net
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
message.setSubject("Testing Subject");
// Now set the actual message
message.setText("Hello, this is sample for to check send "
+ "email using JavaMailAPI ");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author BeytullahC
*/
@XmlRootElement(name = "messageDetail")
public class MessageDetail {
public MessageDetail() {
}
public MessageDetail(String subject, Date sendDate) {
this.subject = subject;
this.sendDate = sendDate;
}
public MessageDetail(String from, String subject, Date sendDate) {
this.from = from;
this.subject = subject;
this.sendDate = sendDate;
}
private String from;
private String subject;
private Date sendDate;
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public Date getSendDate() {
return sendDate;
}
public void setSendDate(Date sendDate) {
this.sendDate = sendDate;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
}
-->
Hiç yorum yok:
Yorum Gönder