我有同样的问题,但它实际上是一个非常简单的修复。你所要做的就是改变
message.setContent(schemeParsed, "text/html" ); //; charset=" + charset);
至
message.setText(schemeParsed, "utf-8", "html");
然后一切都应该工作。如果您认为您的变量可能存在问题,这里是我的所有代码。
import java.io.UnsupportedEncodingException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Drivers {
static Scanner scanly = new Scanner(System.in);
public static String username = "your-email@your-domain.com";
public static String name = "";
public static String password = "your-password";
public static String recipient = "";
public static String subject = "";
public static String emessage = "";
public static String answer = "";
public static String count = "";
public static String wait = "";
public static boolean loop = true;
public static int countint;
public static int waitint;
public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException {
print("Hi! Welcome! Please begin by entering the recipient: ");
recipient = scanly.nextLine();
print("What would you like your name to be?");
name = scanly.nextLine();
print("What would you like the subject line to be?");
subject = scanly.nextLine();
print("Please enter the message:");
emessage = scanly.nextLine();
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.mail.yahoo.com");
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
print("Recipient: " + recipient + "\nSubject: " + subject + "\nMessage: " + emessage);
while(loop)
{
print("Type 's' to send the message, 'c' to cancel, 'n' to change the message/subject, or 'sp' to choose how many.");
answer = scanly.nextLine();
if(answer.toLowerCase().equals("s"))
{
print("Establishing connection..");
MimeMessage message = new MimeMessage(session);
try {
print("Setting up defaults..");
message.setFrom(new InternetAddress(username, name));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
message.setSubject(subject);
message.setText(emessage, "utf-8", "html");
print("Sending..");
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
print("Sent!");
print("Pausing..");
} else if(answer.toLowerCase().equals("c")) {
print("Bye!");
System.exit(0);
} else if(answer.toLowerCase().equals("n")) {
main(args);
} else if(answer.toLowerCase().equals("sp"))
{
print("How many times?");
count = scanly.nextLine();
countint = Integer.parseInt(count);
print("What would you like the wait time to be? (Recommended is 4000-10000)");
wait = scanly.nextLine();
waitint = Integer.parseInt(wait);
for (int i = 0; i < countint;
{
print("Establishing connection..");
Message message = new MimeMessage(session);
try {
print("Setting up defaults..");
message.setFrom(new InternetAddress(username, name));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
message.setSubject(subject);
message.setText(emessage);
print("Sending..");
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
print("Sent!");
Thread.sleep(waitint);
}
} else
{
print("Error..");
}
}
}
public static void print(String ttp)
{
System.out.println(ttp);
}
}
无论如何,如果有任何问题或任何事情,请发送电子邮件至 drenagem-github@outlook.com