Olası Çoğalt:
Gmail'i kullanarak bir Java uygulamasından nasıl e-posta gönderirsiniz?
Java'dan nasıl SMTP Mesajı gönderirim?
İşte Gmail smtp için bir örnek:
import Java.io.*;
import Java.net.InetAddress;
import Java.util.Properties;
import Java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import com.Sun.mail.smtp.*;
public class Distribution {
public static void main(String args[]) throws Exception {
Properties props = System.getProperties();
props.put("mail.smtps.Host","smtp.gmail.com");
props.put("mail.smtps.auth","true");
Session session = Session.getInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("[email protected]"));;
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]", false));
msg.setSubject("Heisann "+System.currentTimeMillis());
msg.setText("Med vennlig hilsennTov Are Jacobsen");
msg.setHeader("X-Mailer", "Tov Are's program");
msg.setSentDate(new Date());
SMTPTransport t =
(SMTPTransport)session.getTransport("smtps");
t.connect("smtp.gmail.com", "[email protected]", "<insert password here>");
t.sendMessage(msg, msg.getAllRecipients());
System.out.println("Response: " + t.getLastServerResponse());
t.close();
}
}
Şimdi, bu şekilde ancak proje bağımlılıklarınızı minimumda tutmak istiyorsanız, aksi halde Apache'den sınıfları kullanmanızı şiddetle tavsiye ederim
http://commons.Apache.org/email/
Saygılarımızla
Tov Jacobsen
Başka bir yol da aspirin kullanmak ( https://github.com/masukomi/aspirin ) şöyle:
MailQue.queMail(MimeMessage message)
... mesajınızı yukarıdaki gibi oluşturduktan sonra.
Aspirin bir smtp 'sunucusu' olduğundan yapılandırmanız gerekmez. Ancak, geniş bir alıcı grubuna e-posta göndermenin, posta sunucularını ve istemci uygulamalarını alan birçok farklı spam filtreleme kuralları nedeniyle göründüğü kadar basit olmadığını unutmayın.
Lütfen bu yazıya bakın
Java uygulamasıyla GMail, Yahoo veya Hotmail kullanarak nasıl e-posta gönderebilirim?
Bu, gmail’e özgüdür ancak smtp kimlik bilgilerinizi değiştirebilirsiniz.
JavaMail API ve ilgili javadocs'a bakınız.
Java Uygulamalarında aşağıdaki eğitime bakın.
import javax.mail.*;
import javax.mail.internet.*;
import Java.util.*;
public void postMail(String recipients[], String subject,
String message , String from) throws MessagingException {
//Set the Host smtp address
Properties props = new Properties();
props.put("mail.smtp.Host", "smtp.jcom.net");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}