我在Eclipse IDE中有一个Gradle项目,并且需要能够将电子邮件回执作为学校项目的一部分发送。我查看了http://www.tutorialspoint.com/java/java_sending_email.htm链接,以尝试创建最基本的电子邮件。我已经尝试过“发送简单的电子邮件”示例,但将其作为我的错误:
Usage - java org.mortbay.jetty.Main [<addr>:]<port>
Usage - java org.mortbay.jetty.Main [<addr>:]<port> docroot
Usage - java org.mortbay.jetty.Main [<addr>:]<port> -webapp myapp.war
Usage - java org.mortbay.jetty.Main [<addr>:]<port> -webapps webapps
Usage - java -jar jetty-x.x.x-standalone.jar [<addr>:]<port>
Usage - java -jar jetty-x.x.x-standalone.jar [<addr>:]<port> docroot
Usage - java -jar jetty-x.x.x-standalone.jar [<addr>:]<port> -webapp myapp.war
Usage - java -jar jetty-x.x.x-standalone.jar [<addr>:]<port> -webapps webapps
我猜我没有正确安装JavaMail API和Java激活框架(JAF)。我已经按照一些指南进行操作。我所做的是右键单击gradle项目->属性-> Java Build Path->库选项卡->添加外部JAR。。然后,我添加了JAF和JavaMail jar文件(activation-1.1.1.jar和mail-1.4 .5.jar)。我也加了
compile group: 'javax.mail', name: 'mail', version: '1.4.5'
compile group: 'javax.activation', name: 'activation', version: '1.1.1'
到我的build.gradle文件。任何有关如何使这项工作的帮助将不胜感激。
这是我使用的代码。
// File Name SendEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "abcd@gmail.com";
// Sender's email ID needs to be mentioned
String from = "web@gmail.com";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
最佳答案
您必须进行适当的设置才能发送电子邮件。喜欢
String host = "localhost"; // instead of localhost you have to give your hostname.
如果要通过gmail发送邮件,请使用以下代码:
SendEmail.java
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail{
public static void main(String[] args) {
final String username = "username@gmail.com";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to-email@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Welcome Message");
Transport.send(message);
System.out.println("Mail Sent Successfully");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
关于java - 在Eclipse的Gradle Project中运行JavaMail API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38025567/