我不是一个非常有经验的编码员,但我一直在学习。现在我正在编写测试电子邮件机器人,以及发送电子邮件。我尝试制作时遇到了问题,因此您可以在JOptionPane对话框中输入邮件和电子邮件的主题。
这是代码,查看顶部的字符串和底部的messageobjs ..
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;
import javax.swing.JOptionPane;
public class Ebot2
{
public static void main(String[] args)
{
String Dest;
Dest = JOptionPane.showInputDialog("Who would you like to message?");
String Subject;
Subject = JOptionPane.showInputDialog("What is the message subject?");
String Message;
Message = JOptionPane.showInputDialog("What is the message?");
String sendrmailid = "email@gmail.com";
final String uname = "email";
final String pwd = "pass";
Properties propvls = new Properties();
propvls.put("mail.smtp.auth", "true");
propvls.put("mail.smtp.starttls.enable", "true");
propvls.put("mail.smtp.host", "smtp.gmail.com");
propvls.put("mail.smpt.port", "25");
Session sessionobj = Session.getInstance(propvls,
new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(uname, pwd);
}
});
try
{
Message messageobj = new MimeMessage(sessionobj);
messageobj.setFrom(new InternetAddress(sendrmailid));
messageobj.setRecipients(Message.RecipientType.TO,InternetAddress.parse(Dest));
messageobj.setSubject(Subject);
messageobj.setText(Message);
Transport.send(messageobj);
System.out.println("Your email sent successfully....");
}
catch (MessagingException exp)
{
throw new RuntimeException(exp);
}
}
}
抱歉格式化,代码块很难。在我将setSubject和setText更改为通过JOptionPane输入的字符串后,无论如何错误我开始了。错误是..
Ebot2.java:53: error: cannot find symbol
messageobj.setRecipients(Message.RecipientType.TO,InternetAddress.parse(Dest));
^
symbol: variable RecipientType
location: variable Message of type String
1 error
感谢任何回答的人,我真的需要帮助!
答案 0 :(得分:0)
我修好了。问题是我有消息的字符串设置为消息(与主题相同的问题),这也是一个变量。所以我只是重命名了字符串,一切正常。无论如何,感谢您的帮助,很高兴知道我可以到这个网站提问。
接下来,我将尝试弄清楚如何多次发送电子邮件,但我应该能够自己解决这个问题。