如何将我通过javamail编写的消息存储到MySQL表中?我已经配置james服务器配置文件连接到MySQL服务器(数据源元素名称为maildb),我将<inboxRepository>James服务器配置文件中的元素更改为
<inboxRepository>
<repository destinationURL="db://maildb/spammer/"
type="MAIL"/>
</inboxRepository>
Run Code Online (Sandbox Code Playgroud)
但是我仍然无法从MySql中的邮件数据库中的垃圾邮件发送者表的收件箱列中读取邮件.
这是我的javamail类:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class mail{
public static void main(String[] argts){
String to = "red@localhost";
String from = "blue@localhost";
String subject = "jdk";
String body = "Down to wind";
if ((from != null) && (to != null)
&& (subject != null) && (body != null))
// we have mail to send
{
try {
Properties props = new Properties();
props.put("mail.host", "127.0.0.1 ");
props.put("mail.smtp.auth","true"); …Run Code Online (Sandbox Code Playgroud) 几个星期以来,我一直在为android开发一个电子邮件客户端,我一直忽略解析电子邮件内容一段时间,因为我从来没有能够让它工作.因此,现在是时候寻求帮助了!
我一直在环顾四周,我遇到过一些我尝试过的方法,但从来没有取得多大成功!目前我最接近的尝试必须是:
private String parseContent(Message m) throws Exception
{
//Multipart mp = (Multipart)c;
//int j = mp.getCount();
/*for (int i = 0; i < mp.getCount(); i++)
{
Part part = mp.getBodyPart(i);
System.out.println(((MimeMessage)m).getContent());
content = content + part.toString();
//System.out.println((String)part.getContent());
}*/
Object content = m.getContent();
String contentReturn = null;
if (content instanceof String)
{
contentReturn = (String) content;
}
else if (content instanceof Multipart)
{
Multipart multipart = (Multipart) content;
BodyPart part = multipart.getBodyPart(0);
part.toString();
contentReturn = part.getContent().toString();
}
return contentReturn;
} …Run Code Online (Sandbox Code Playgroud) 事实证明,JavaMail比我想象的更令人沮丧.我在网上看了几个关于如何通过Gmail服务器发送简单SMTP电子邮件的例子(但不是通过SSL).在尝试了几个不同的代码示例后,我在调用时始终以相同的示例异常结束transport.connect().我一直得到这个堆栈跟踪:
Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. l10sm302158wfk.21
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097)
at SendEmail.main(SendEmail.java:47)
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我应该添加或做什么来解决这个问题?
这是我的代码:
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.host", "smtp.gmail.com");
props.put("mail.user", "blahblah@gmail.com");
props.put("mail.password", "blah");
props.put("mail.port", "587");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("This is a test");
message.setContent("This is a test", "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress("blahblah2@gmail.com"));
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
Run Code Online (Sandbox Code Playgroud) 我不明白为什么我得到这个例外.这是尝试发送电子邮件的代码.
public void sendAsHotmail() {
final String username = jTextField14.getText();
final String password = jPasswordField4.getText();
String subject = jTextField16.getText();
String Cc = jTextField17.getText();
String Bcc = jTextField18.getText();
String recipient = jTextArea5.getText();
Properties props = new Properties();
props.put( "mail.smtp.host" , "smtp.live.com");
props.put( "mail.smtp.user" , username );
// Use TLS
props.put("mail.smtp.auth" , "true" );
props.put( "mail.smtp.starttls.enable" , "true" );
props.put( "mail.smtp.password" , password );
Session session = Session.getDefaultInstance( props , new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if( username == null …Run Code Online (Sandbox Code Playgroud) 我的任务是为Ant构建脚本创建自动通知.这样,当有人推出部署时,我们的团队可以自动收到有关它的电子邮件.
显而易见的选择是使用Ant的邮件任务,该任务是使用Ant预定义的:
<target name="notify" description="notify team">
<mail subject="latest deployment">
<from address="me@gmail.com" />
<to address="jimmy@yahoo.com" />
<message>A new build has been pushed out to prod</message>
</mail>
</target>
Run Code Online (Sandbox Code Playgroud)
但是,这会导致以下运行时异常:
java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage
Run Code Online (Sandbox Code Playgroud)
这是因为Ant的邮件任务依赖于JavaMail和JavaBeans Activation Framework,这些库显然不包含在其发行版中.为什么Ant会定义依赖于库但不包含该库的任务我不清楚.
此帖已经讨论过这个问题:在外部位置使用邮件和激活jar的ant邮件任务.基于答案,似乎有两种解决方案.
第一种是手动将这些库依赖项放在ant类路径上.这可以使用-lib命令行参数完成,或者在eclipse中可以使用Window > Preferences > Ant > Runtime > Global Entries,然后添加mail.jar和activation.jar(我很确定这相同的事情-lib,如果我错了,请纠正我).但是这个解决方案对我们的团队来说是不受欢迎的,因为这意味着我们每个人都必须手动执行这些步骤.我正在寻找一种方法来简单地提交我的通知代码,它应该在svn更新后的另一个eclipse安装上工作.
链接文章中的另一个解决方案提到了一种通过从自身调用Ant来以编程方式执行上述操作的方法:
<exec executable="ant">
<arg value="-lib"/>
<arg value="PATH_TO_MY_LIB"/>
<arg value="target"/>
</exec>
Run Code Online (Sandbox Code Playgroud)
这个问题是Ant命令行工具显然只包含在完整安装中,而不是eclipse发行版.因此,如果没有任何想要使用邮件任务的人进行手动操作,就无法使其工作.
有没有什么方法可以自动化这个,而不会给项目设置添加另一个恼人的步骤?我真的不明白为什么这么难实现 - 看起来如果邮件任务没有 …
有没有办法使用JavaMail API来检查所使用的邮件服务器是否存活?如果没有,Java代码怎么做?
谢谢你的帮助.
我在这里和其他地方搜索了几个相关的帖子,但没有一个解决了我的问题.我有一个程序使用"javamail API"向一组人发送电子邮件.它工作得很好.今天我再次需要,但我无法发送任何电子邮件...我的sendEmail方法如下:
public void sendEmail(String userName, String password, String toAddress,
String subject, String message, String[] attachFiles)
throws AddressException, MessagingException {
// sets SMTP properties
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.user", userName);
properties.put("mail.password", password);
// creates a new session with an authenticator
Authenticator auth = new SMTPAuthenticator(userName, password);
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(userName, "My name"));
} catch (UnsupportedEncodingException …Run Code Online (Sandbox Code Playgroud) 这是我的代码
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailSendClass {
public static void main (String [] args){
// Recipient's email ID needs to be mentioned.
String to = "abc82@gmail.com";
// Sender's email ID needs to be mentioned
String from = "xyz@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 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用org.springframework.mail.javamail.JavaMailSenderImpl发送一封非常简单的电子邮件.以下是代码:
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(request.getCustomerEmail());
mailMessage.setSubject("someSubject");
mailMessage.setFrom("vincent@myDomain.com");
mailSender.send(mailMessage);
Run Code Online (Sandbox Code Playgroud)
这是我得到的例外:
Caused by: org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: No MimeMessage content
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:459)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:307)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296)
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么会发生这种情况..
任何的想法 ?
我有两个相似的代码部分,用java和php编写.由于证书错误,PHP没有发送电子邮件 -
Connection failed. Error #2: stream_socket_enable_crypto():
Peer certificate CN=*.hosting.com' did not match
expected CN=smtp.anotherhosting.com'
Run Code Online (Sandbox Code Playgroud)
但java代码发送电子邮件没有任何问题,我不明白为什么.(从任何地方我都看到问题 - 如何跳过使用java的ssl检查?
这是代码:
PHP:
<?php
require './PHPMailer.php';
require './SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 4;
$mail->isSMTP();
$mail->Host = 'smtp.anotherhosting.com';
$mail->SMTPAuth = true;
$mail->Username = 'username@anotherhosting.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
//Recipients
$mail->setFrom('from@company.com');
$mail->addAddress('myemail@company.com');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject12';
$mail->Body = 'This is the HTML message bo22dy <b>in bold!</b>';
$mail->send();
echo 'Message has been …Run Code Online (Sandbox Code Playgroud)