#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#using <System.dll>
using namespace System;
using namespace System::Net::Sockets;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::Text;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::Mail;
using namespace System::Net::Mime;
using namespace System::Threading;
using namespace System::ComponentModel;
namespace GMailSend
{
//array: http://msdn.microsoft.com/zh-tw/library/ms235236.aspx
void Send(String^ from, String^ to, String^ subject, String^ body,
array<System::String ^>^ cc, int cc_num, array<System::String ^>^ bcc, int bcc_num, String^ host, int port,
String^ smtpUsername, String^ smtpPassword)
{
// Create mail message
MailMessage^ message = gcnew MailMessage(from, to, subject, body);
message->BodyEncoding = System::Text::Encoding::UTF8;
if (cc != nullptr && cc_num > 0)
{
for(int i=0; i<cc_num; i++)
{
message->CC->Add(gcnew MailAddress(cc[i]));
}
}
if (bcc != nullptr && bcc_num > 0)
{
for(int i=0; i<bcc_num; i++)
{
message->Bcc->Add(gcnew MailAddress(bcc[i]));
}
}
// Send email
SmtpClient^ client = gcnew SmtpClient(host, port);
if (!String::IsNullOrEmpty(smtpUsername) && !String::IsNullOrEmpty(smtpPassword))
{
client->Credentials = gcnew NetworkCredential(smtpUsername, smtpPassword);
}
client->EnableSsl = true;
client->Send(message);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
GMailSend::Send("from@gmail.com","to@hotmail.com", "testsubject", "testbodymessage", nullptr, 0, nullptr, 0,
"smtp.gmail.com", 587, "gmail_account", "gmail_password");
system("pause");
return 0;
}
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#using <System.dll>
using namespace System;
using namespace System::Net::Sockets;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::Text;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::Mail;
using namespace System::Net::Mime;
using namespace System::Threading;
using namespace System::ComponentModel;
#define GMail_DEBUG
String^ showUsage() {
StringBuilder^ sb = gcnew StringBuilder();
sb->AppendLine( "Usage: MailSharp [options] ToAddress" );
sb->AppendLine( "\t-a File to attach." );
sb->AppendLine( "\t-f From address" );
sb->AppendLine( "\t-b Body message" );
sb->AppendLine( "\t-s Subject");
sb->AppendLine( "\t-S SMTP server host" );
sb->AppendLine( "\t-U Username for SMTP server authentication" );
sb->AppendLine( "\t-p Password for SMTP server authentication" );
sb->AppendLine( "\t-P SMTP server port" );
sb->AppendLine( "\t-l Use SSL" );
sb->AppendLine( "\t-h Help/Usage");
sb->AppendLine();
sb->AppendLine( "Example:" );
sb->AppendLine( "\tFor GMail" );
sb->AppendLine( "\tMailSharp -f your_name@gmail.com -b MessageBody -s TestSubject -S smtp.gmail.com -P 587 -l -U your_gmail -p your_gmail_password -t someone@somewhere.com" );
#ifdef GMail_DEBUG
// Display the number of characters in the StringBuilder and its string.
Console::WriteLine("{0} chars: {1}", sb->Length, sb->ToString());
#endif
return sb->ToString();
}
int _tmain(int argc, _TCHAR* argv[])
{
//showUsage();
MailAddress^ from = nullptr;
MailAddress^ to = nullptr;
MailMessage^ message = gcnew MailMessage();
SmtpClient^ client = gcnew SmtpClient();
NetworkCredential^ myCred = gcnew NetworkCredential();
bool bShowUsage=false;
try{
if ( argc > 0 ) {
for ( int i=0; i<argc; i++ ) {
if( argv[i][0] == '-' ){
switch ( argv[i][1] ) {
case 'S': // server host
i++;
if ( i<argc ) {
client->Host = gcnew String(argv[i]);
} else
throw gcnew Exception( "-S was specified, but no value." );
break;
case 'P': // server port
i++;
if ( i<argc ) {
client->Port= Convert::ToInt32( gcnew String(argv[i]) );
} else
throw gcnew Exception( "-P was specified, but no value." );
break;
case 'T': // timeout
i++;
if ( i<argc ) {
client->Timeout = Convert::ToInt32( gcnew String(argv[i]) );
} else
throw gcnew Exception( "-T was specified, but no value." );
break;
case 'U': // username for smtp server authentication
i++;
if ( i<argc )
myCred->UserName = gcnew String(argv[i]);
else
throw gcnew Exception( "-U was specified, but no value." );
break;
case 'p': // password for smtp server authentication
i++;
if ( i<argc )
myCred->Password = gcnew String(argv[i]);
else
throw gcnew Exception( "-p was specified, but no value." );
break;
case 'l': // use SSL
client->EnableSsl = true;
break;
case 's': // subject
i++; // next one is subject.
if ( i<argc ) {
message->Subject = gcnew String(argv[i]);
message->SubjectEncoding = System::Text::Encoding::UTF8;
} else
throw gcnew Exception( "-s was specified, but no value." );
break;
case 'a': // attachment
i++; // next one is attachment filename.
if ( i<argc ) {
// Add attachment.
Attachment^ data = gcnew Attachment( gcnew String(argv[i]), MediaTypeNames::Application::Octet);
message->Attachments->Add(data);
} else
throw gcnew Exception( "-a was specified, but no value." );
break;
case 'b': // body message.
i++; // next one is body message
if ( i<argc ) {
message->Body = gcnew String(argv[i]);
message->BodyEncoding = System::Text::Encoding::UTF8;
} else
throw gcnew Exception( "-b was specified, but no value." );
break;
case 'f': // from address
i++;
if ( i<argc ) {
// Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
// from = new MailAddress( "someone@gmail.com", "someone", System.Text.Encoding.UTF8);
from = gcnew MailAddress( gcnew String(argv[i]) );
} else
throw gcnew Exception( "-f was specified, but no value." );
break;
case 'h': // show help/usage
bShowUsage=true;
break;
case 't':
i++;
to = gcnew MailAddress( gcnew String(argv[i]) );
break;
}//end of switch
}//end of check '-'
}//end of for
} else
throw gcnew Exception("No arguments.");
}catch(Exception^ ex){
Console::WriteLine( ex->Message );
bShowUsage = true;
}
try {
if ( bShowUsage == true )
throw gcnew Exception( showUsage() );
if ( from==nullptr )
throw gcnew Exception( "Must specify from address (-f)." );
// Set destinations for the e-mail message.
if ( to == nullptr )
throw gcnew Exception("At least, must specify to address");
if ( client->Host == String::Empty )
throw gcnew Exception("Must specify SMTP Server (-S)." );
// Specify the message content.
message->From = from;
message->To->Add( to );
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
//client.UseDefaultCredentials = false;
client->Credentials = myCred;
// Send.
// If you need asynchronous sample, please visit the reference above.
client->Send(message);
Console::WriteLine("Done.");
} catch ( Exception^ ex ) {
Console::WriteLine( "Exception was raised when sending...");
Console::WriteLine( ex->Message );
} finally {
// Clean up.
message->~MailMessage();
}
system("pause");
return 0;
}
0 意見:
張貼留言