0
Я получаю синтаксис для строки ниже, пытаясь отправить почту через SMTP. Я пытался в google, но не получил никакого релевантного ответа.получение синтаксической ошибки в property.setProperty ("mail.smtp.host", хост);
property.setProperty ("mail.smtp.host", host);
КОД:
public class SendMail {
//Recipient Mail id
String to = "Receiver Mail ID";
//Sender Mail Id
String from = "Sender Mail ID";
//Sending email from the localhost
String host = "localhost";
//Get System Properties
Properties property = System.getProperties();
//Setup the mail server
property.setProperty("mail.smtp.host",host);
property.setProperty("mail.smtp.port" , "465");
//property.put("mail.smtp.auth", "true");
//Get the default session object
Session session = Session.getDefaultInstance(property);
try
{
//Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
//Set From: header field of the header
message.setFrom(new InternetAddress(from));
//Set To : header field of the header
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//Set Subject : header field
message.setSubject("Automation Testing Report");
//Create the Message part
BodyPart messageBodypart = new MimeBodyPart();
//Enter the message in the Mail Body
messageBodypart.setText("***********Find the below for the Report****************");
//Create a Multipart Message
Multipart multipart = new MimeMultipart();
//Set Text message part
multipart.addBodyPart(messageBodypart);
//Part two is attachment
/*create the Message part*/
messageBodypart = new MimeBodyPart();
String filename = "E:\\Project\\jar\\Selenium Scripts\\Hybrid_Driven\\test-output\\emailable-report.html";
DataSource source = new FileDataSource(filename);
messageBodypart.setDataHandler(new DataHandler(source));
messageBodypart.setFileName(filename);
//set the text message part
multipart.addBodyPart(messageBodypart);
//Send the complete message part
message.setContent(multipart);
//Send message
Transport.send(message);
System.out.println("Mail has sent successfully");
}
catch(MessagingException mex)
{
mex.printStackTrace();
}
}
}
Пожалуйста, помогите мне решить эту проблему.
Ya super, он исправлен. Но в чем причина. Мы не должны упоминать об этом в определении класса? –
Определения классов просто не предназначены для выполнения кода в java. Это кажется другим на других языках, например. Скала. Но код в определении класса в scala неявно является частью конструктора по умолчанию. В основном, определение класса определяет, какие поля и методы-члены имеют класс. Конструкторы и методы могут затем работать с этими полями и методами. –
Благодарим вас за разъяснение. Еще одно, выполнив этот класс, я не получаю электронную почту как output.i не знаю, выполняется ли это или нет. Как найти, что .Я использую модель POM –