我使用springmail从我的smtp服务器发送带有以下配置的电子邮件:
<bean id="springEmailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="defaultEncoding" value="UTF-8"/>
<property name="host" value="mail.myserver.com"/>
<property name="port" value="25"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
<property name="javaMailProperties">
<value>
mail.debug=true
mail.smtp.auth=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
</value>
</property></bean>
Run Code Online (Sandbox Code Playgroud)
但它抛出"javax.net.ssl.SSLException:无法识别的SSL消息,明文连接?" 我已经在端口465上用gmail测试了这个配置,但它确实有效.
请告诉我我做错了什么.谢谢
我想在Android上获取我的Exchange电子邮件,为此我使用javamail api for android ...它在使用imap的gmail和yahoo上工作得很好.问题是我的交换服务器有自签名证书所以android不太喜欢这个,我得到了03-14 12:46:13.698: WARN/System.err(281): javax.mail.MessagingException: Not trusted server certificate;
我已经看到了这个例子:使用JavaMail API在Android中发送电子邮件而不使用默认/内置应用程序,其中有人通过ssl发送示例..我想我可以使用JSSEProvider接受我的自签名证书但我不知道我知道如何使用它.
请帮我!
我有一个示例gmail客户端类.在该类中,我初始化了两个IMAPS和SMTPS会话.有一段时间它工作正常.但现在Gmail会抛出SMTO发送失败例外.
以下是我通过javamail连接Gmail的方法.
//String smtpHost = "imap.gmail.com";
//String imapHost = "smtp.gmail.com";
//String userName = "XXXXXX@gmail.com";
//String userPass = "PASSWORD";
//Properties props = new Properties();
public DirectMailBox(String smtpHost, String imapHost, String userName,
String userPass, Properties props) throws MessagingException {
super();
this.imapHost = imapHost;
this.smtpHost = smtpHost;
this.userName = userName;
this.userPass = userPass;
this.props = props;
session = Session.getDefaultInstance(props);
session.setDebug(true);
imapsStore = session.getStore("imaps");
imapsStore.connect(imapHost, userName, userPass);
smtpsTransport = session.getTransport("smtps");
smtpsTransport.connect(smtpHost, userName, userPass);
smtpsTransport.addTransportListener(this);
}
Run Code Online (Sandbox Code Playgroud)
这是我如何调用sendmail ..
public void sendMail(MimeMessage newMessage) throws MessagingException { …Run Code Online (Sandbox Code Playgroud) 将邮件会话(javax.mail.Session)存储在单例中是一种很好的做法吗?我和我的团队决定在Singleton类中的一个静态变量中保留一个邮件会话.
所以,在私有构造函数中,我们这样做:
try {
Properties props = new Properties();
props.put("mail.transport.protocol", config.getMailTransportProtocol());
props.put("mail.smtp.starttls.enable", config.getMailStarttlsEnable());
props.put("mail.smtp.host", config.getMailHost());
props.put("mail.smtp.auth", config.getMailAuth());
props.put("mail.smtp.user", config.getMailFrom());
props.put("mail.debug", config.getMailDebug());
props.put("mail.smtp.port", config.getMailPort());
props.put("mail.smtp.socketFactory.port", config.getMailPort());
props.put("mail.smtp.socketFactory.class", config.getMailSocketFactoryClass());
props.put("mail.smtp.socketFactory.fallback", config.getMailSocketFactoryFallback());
props.put("mail.pop3.host", config.getMailPop3Host());
props.put("mail.store.protocol", config.getMailStoreProtocol());
SimpleAuth auth = new SimpleAuth(config.getMailUser(), config.getMailPass());
MailSession.session = Session.getDefaultInstance(props, auth);
session.setDebug(config.getMailDebug());
} catch (Throwable ex) {
System.err.println("Initial MailSession creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
Run Code Online (Sandbox Code Playgroud)
但我担心最好是保持这样,还是为每封电子邮件打开和关闭一个会话.
public class SendMail {
private class SMTPAuthenticator extends javax.mail.Authenticator
{
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("userID", "pwd");
}
}
public void sendMail() throws Exception {
String strFromIds = "xyz@gmail.com";
String strToIds = "xyz@domain.com";
String strSubject = "Sample Mail Subject.";
String strContent = "Sample Mail Content";
Properties objProperties = System.getProperties();
objProperties.put("mail.smtp.host", "<smtp host name>");
objProperties.put("mail.smtp.port", "25");
objProperties.put("mail.transport.protocol", "smtp");
objProperties.put("mail.smtp.submitter", "<user id>");
objProperties.put("mail.smtp.auth", true);
objProperties.put("mail.debug", "true");
Session objSMTPSession = Session.getDefaultInstance(objProperties, new
SMTPAuthenticator());
Message objMessage = new MimeMessage(objSMTPSession);
objMessage.setFrom(new InternetAddress(strFromIds));
InternetAddress[] …Run Code Online (Sandbox Code Playgroud) 我想要获取最近1小时内收到的邮件.我在这里查了一下 -
并尝试以下 -
DateTime current = new DateTime();
DateTime oneHourLess = current.minusHours(1);
SearchTerm olderThan = new ReceivedDateTerm(ComparisonTerm.LT, current.toDate());
SearchTerm newerThan = new ReceivedDateTerm(ComparisonTerm.GT, oneHourLess.toDate());
SearchTerm andTerm = new AndTerm(olderThan, newerThan);
Message totalMess[] = inbox.search(andTerm);
Run Code Online (Sandbox Code Playgroud)
但由于ReceivedDateTerm没有花时间考虑,我没有收到任何邮件.
有什么方法可以做到这一点吗?
我目前正在开发一个发送电子邮件的java webapp.由于我在Tomcat 7上部署它,因此我mail/Session在我的context.xml文件上配置了Resource ,并将最新版本复制mail.jar到Tomcat的lib文件夹中.
当我部署我的Web应用程序并运行它时,我得到以下异常:
java.lang.ClassCastException: javax.mail.Session cannot be cast to javax.mail.Session
Run Code Online (Sandbox Code Playgroud)
这是我用来在我班上发送电子邮件的代码:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
Session session = (Session) envCtx.lookup("mail/Session");
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress toIA[] = new InternetAddress[1];
toIA[0] = new InternetAddress(to);
message.setRecipients(Message.RecipientType.TO, toIA);
message.setSubject(subject);
message.setContent(mensagem, "text/plain");
Transport.send(message);
Run Code Online (Sandbox Code Playgroud)
我知道这可能是因为我mail.jar在Tomcat lib文件夹和我的web app lib文件夹中都有,我的问题是我不能简单地从Maven中删除依赖项pom.xml,因为如果我这样做,Eclipse会说它找不到某些类(如Session).
如果我的webapp中没有相应的JAR,我该如何使用负责发送电子邮件的类?
请帮我解决这个问题.
我们使用java应用程序向用户发送邮件.
现在,我们要实现以下功能.
请帮我怎么做.
谢谢你的帮助.
我正在尝试将电子邮件直接发送到目标MX服务器,避免使用中继smtp服务器.有条理地,可以让名称服务器列表对dns服务器进行查询.因此,使用此类http://www.eyeasme.com/Shayne/MAILHOSTS/mailHostsLookup.html,我可以获得域的邮件交换服务器列表.
那么,一旦我有了,我该如何继续发送电子邮件?我应该使用javax.mail或如何?如果是,我该如何配置呢?
我正在尝试从Java发送邮件. 如果我将邮件发送到同一个SMTP它工作正常. 如果我将邮件发送到外部SMTP,如Gmail,雅虎等,它会显示错误,如,
[com.sun.mail.smtp.SMTPAddressFailedException:550 5.7.1无法中继] [1]
错误:
SimpleEmail Start
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay
Mail Send Successfully
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:2064)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1286)
at javax.mail.Transport.send0(Transport.java:255)
at javax.mail.Transport.send(Transport.java:124)
at com.nirav.java.project.demo.JavaMailSend.sendEmail(JavaMailSend.java:26)
at com.nirav.java.project.demo.NewSimpleMail.main(NewSimpleMail.java:34)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1917)
... 5 more
Run Code Online (Sandbox Code Playgroud)
邮件发送代码:
try {
System.out.println("SimpleEmail Start");
String smtpHostServer = "XX.XX.XX.XXX";
final String toEmail = "XXXXXXXXXX@XXX.XXX";
final String fromEmail = "XXXXXXXXXX@XXX.XXX";
final String password = "XXXXXXXXXXXX";
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHostServer);
props.put("mail.smtp.port", …Run Code Online (Sandbox Code Playgroud)