0

Please have a look at the following code

Email.h

    #pragma once
    #include <string>

    using namespace System::Net;
    using namespace System::Net::Mail;
    using namespace System::Net::Mime;

    using namespace std;


    ref class Email
    {
    public:
        Email();

        //Sets the location of the attachment
        void setAttachment(System::String ^attachmentLocation);

        //Set the the from address of the email
        void setFromAddress(System::String ^fromAddress);

        //Set the reception's address of the email
        void setToAddress(System::String ^toAddress);

        //Set the subject pf the email
        void setSubject(System::String ^subject);

        //Set the Message of the email
        void setMessage(System::String ^emailMessage);

        //Set the SMTP client
        void setSMTPClient(System::String ^smtpClient);

        //Send the email
        void sendEmail();

    private:
        MailMessage ^mail;
        Attachment ^attachment;
        SmtpClient ^client;
    };

Email.cpp

#include "StdAfx.h"
#include "Email.h"


Email::Email()
{
    mail = gcnew MailMessage();

}

void Email::setAttachment(System::String ^attachmentLocation)
{
    attachment = gcnew Attachment(attachmentLocation);

    ContentDisposition ^disposition = attachment->ContentDisposition;
    disposition->CreationDate = System::IO::File::GetCreationTime(attachmentLocation);
    disposition->ModificationDate = System::IO::File::GetLastWriteTime(attachmentLocation);
    disposition->ReadDate = System::IO::File::GetLastAccessTime(attachmentLocation);
}

void Email::setFromAddress(System::String ^fromAddress)
{
    mail->From = gcnew MailAddress(fromAddress);
}

void Email::setMessage(System::String ^emailMessage)
{
    mail->Body = emailMessage;
}

void Email::setSMTPClient(System::String ^smtpClient)
{
    client = gcnew SmtpClient(smtpClient);
}

void Email::setSubject(System::String ^subject)
{
    mail->Subject = subject;
}

void Email::setToAddress(System::String ^toAddress)
{
    mail->To->Add(toAddress);
}

void Email::sendEmail()
{
    try
    {
        client->Send(mail);
    }
    catch(System::Exception ^e)
    {
        System::Windows::Forms::MessageBox::Show(e->ToString());
    }

}

This is how I call this class, in another class

Email ^email = gcnew Email();
                     email->setToAddress("sss@yahoo.com");
                     email->setSubject("Intruder Detected");
                     email->setSMTPClient("localhost");
                     email->setMessage("sending mail. Image is attached");
                     email->setFromAddress("justsomemail@lol.com");
                     email->setAttachment("D:/OpenCV Final Year/Images/intruder.bmp");
                     email->sendEmail();

Please note that From address here is a non existing/not real address. In real code also it is like the same. To address is real and modified here because I can't publish the original address.

When I run this code, I get the following error.

enter image description here

This is my very first attempt in C++/CLI (C++ AND C#) Email sending. I can remember I used localhost for my Java applications in Java Mail so I really can't find what is the real error here.

4

0 回答 0