简体   繁体   English

如何使用apache james电子邮件服务器向gmail发送邮件

[英]how to send mail using apache james email server to gmail

I am trying to send mail using apache james server. 我正在尝试使用Apache James服务器发送邮件。 I have done with all james configuration. 我已经完成了所有詹姆斯的配置。
My code executes correctly there is no exception. 我的代码正确执行,没有例外。 But mail could not deliver. 但是邮件无法传递。
Here is my sample code 这是我的示例代码

public void sendMail(String toField, String subject, Users user, 
                               HttpServletRequest request)throws Exception {
    // TODO Auto-generated method stub
    logger.info("sending mail....");
    String loginLink = request.getScheme()  +"://" + request.getServerName() 
     + ":" + request.getServerPort() + request.getContextPath()+"/auth/login";

    // Prepare the evaluation context 
    final WebContext ctx = new WebContext(request, request.getSession()
                                    .getServletContext(),request.getLocale());

    ctx.setVariable("eagletId", user.getEagletId()); 
    ctx.setVariable("name", user.getFirstName());
    ctx.setVariable("setSentDate", new Date()); 
    ctx.setVariable("password", user.getPassword()); 
    ctx.setVariable("link",loginLink);

    // Create a mail session  
    Properties properties = new Properties();  
    properties.put("mail.smtp.host", "localhost");  
    properties.put("mail.smtp.port", "25");  
    properties.put("mail.smtp.username", "coepleap");  
    properties.put("mail.smtp.password", "coepleap");  
    Session session = Session.getDefaultInstance(properties,new Authenticator() {
        protected PasswordAuthentication getpassAuthentication(){
            return new PasswordAuthentication("coepleap", "coepleap");
        }
    });

    MimeMessage message = new MimeMessage(session);
    MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
    message.setFrom(new InternetAddress("coepleap"));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(toField));
    helper.setSubject(subject);

    final String htmlContent = this.templateEngine.process("email.html",ctx);
    helper.setText(htmlContent,true);
    Transport.send(message);        
 }

}

anyone who can help me out? 谁能帮助我?

this is code to send mail using gmail 这是使用gmail发送邮件的代码

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 SendMail {

public static void main(String[] args) {

    final String username = "username";
    final String password = "fghdf";

    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 user"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("to user"));
        message.setSubject("Testing Subject");
        message.setText("Dear user ,"
            + "\n\n your username is xxx and pasword is yyy");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

} }

Most likely you're not meeting Gmails requirements for delivery. 您很可能不符合Gmail的交付要求。 It's probably not being able to resolve the sender via rdns and bounching it. 可能无法通过rdns解析发件人并将其启动。 Probably think you're a spammer. 可能认为您是垃圾邮件发送者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM